+
+ g_assert (sizeof (gunichar) == sizeof (Unicode));
+ ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
+ &ucs4_len);
+
+ if (pdf_document->search &&
+ pdf_document->search->ucs4_len == ucs4_len &&
+ memcmp (pdf_document->search->ucs4,
+ ucs4,
+ sizeof (gunichar) * ucs4_len) == 0) {
+ /* Search is unchanged */
+ g_free (ucs4);
+ return;
+ }
+
+ if (pdf_document->search) {
+ pdf_document_search_free (pdf_document->search);
+ pdf_document->search = NULL;
+ }
+
+ search = g_new0 (PdfDocumentSearch, 1);
+
+ search->ucs4 = ucs4;
+ search->ucs4_len = ucs4_len;
+
+ search->current_page_results = g_array_new (FALSE,
+ FALSE,
+ sizeof (EvFindResult));
+ n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
+
+ /* This is an array of bool; with the first value ignored
+ * so we can index by the based-at-1 page numbers
+ */
+ search->other_page_flags = g_new0 (guchar, n_pages + 1);
+
+ search->document = pdf_document;
+
+ /* We add at low priority so the progress bar repaints */
+ search->idle = g_idle_add_full (G_PRIORITY_LOW,
+ pdf_document_search_idle_callback,
+ search,
+ NULL);
+
+ search->output_dev = 0;
+
+ search->start_page = pdf_document->page;
+ search->search_page = search->start_page + 1;
+ if (search->search_page > n_pages)
+ search->search_page = 1;
+
+ search->current_page = -1;
+
+ pdf_document->search = search;
+
+ /* Update for the current page right away */
+ pdf_document_search_page_changed (search);
+}
+
+static void
+pdf_document_find_cancel (EvDocumentFind *document)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document);
+
+ if (pdf_document->search) {
+ pdf_document_search_free (pdf_document->search);
+ pdf_document->search = NULL;
+ }
+}
+
+static void
+pdf_document_search_free (PdfDocumentSearch *search)
+{
+ if (search->idle != 0)
+ g_source_remove (search->idle);
+
+ if (search->output_dev)
+ delete search->output_dev;
+
+ g_array_free (search->current_page_results, TRUE);
+ g_free (search->other_page_flags);
+
+ g_free (search->ucs4);
+ g_free (search);
+}
+
+static void
+pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
+{
+ PdfDocument *document = PDF_DOCUMENT (exporter);
+
+ if (document->ps_out)
+ delete document->ps_out;
+
+ document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
+ document->doc->getCatalog(), 1,
+ ev_document_get_n_pages (EV_DOCUMENT (document)),
+ psModePS);
+}
+
+static void
+pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
+{
+ PdfDocument *document = PDF_DOCUMENT (exporter);
+
+ document->doc->displayPage (document->ps_out, page,
+ 72.0, 72.0, 0, gTrue, gFalse);
+}
+
+static void
+pdf_document_ps_export_end (EvPSExporter *exporter)
+{
+ PdfDocument *document = PDF_DOCUMENT (exporter);
+
+ delete document->ps_out;
+ document->ps_out = NULL;
+}
+
+
+/* EvDocumentBookmarks Implementation */
+typedef struct
+{
+ /* goo GList, not glib */
+ GList *items;
+ int index;
+ int level;
+} BookmarksIter;
+
+static gchar *
+unicode_to_char (OutlineItem *outline_item,
+ UnicodeMap *uMap)
+{
+ GString gstr;
+ gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
+ int i, n;
+
+ for (i = 0; i < outline_item->getTitleLength(); ++i) {
+ n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
+ gstr.append(buf, n);
+ }
+
+ return g_strdup (gstr.getCString ());
+}
+
+
+static gboolean
+pdf_document_bookmarks_has_document_bookmarks (EvDocumentBookmarks *document_bookmarks)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
+ Outline *outline;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
+
+ outline = pdf_document->doc->getOutline();
+ if (outline->getItems() != NULL &&
+ outline->getItems()->getLength() > 0)
+ return TRUE;
+
+ return FALSE;
+}
+
+static EvDocumentBookmarksIter *
+pdf_document_bookmarks_begin_read (EvDocumentBookmarks *document_bookmarks)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
+ Outline *outline;
+ BookmarksIter *iter;
+ GList *items;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), NULL);
+
+ outline = pdf_document->doc->getOutline();
+ items = outline->getItems();
+ if (! items)
+ return NULL;
+
+ iter = g_new0 (BookmarksIter, 1);
+ iter->items = items;
+ iter->index = 0;
+ iter->level = 0;
+
+ return (EvDocumentBookmarksIter *) iter;
+}
+
+static gboolean
+pdf_document_bookmarks_get_values (EvDocumentBookmarks *document_bookmarks,
+ EvDocumentBookmarksIter *bookmarks_iter,
+ char **title,
+ EvDocumentBookmarksType *type,
+ gint *page)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
+ BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
+ OutlineItem *anItem;
+ LinkAction *link_action;
+ LinkDest *link_dest = NULL;
+ LinkURI *link_uri = NULL;
+ LinkGoTo *link_goto = NULL;
+ GString *named_dest;
+ Unicode *link_title;
+ Ref page_ref;
+ gint page_num = -1;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
+ g_return_val_if_fail (title != NULL, FALSE);
+ g_return_val_if_fail (type != NULL, FALSE);
+ g_return_val_if_fail (page != NULL, FALSE);
+
+ anItem = (OutlineItem *)iter->items->get(iter->index);
+ link_action = anItem->getAction ();
+ link_title = anItem->getTitle ();
+
+ if (link_action) {
+ switch (link_action->getKind ()) {
+
+ case actionGoTo:
+ link_goto = dynamic_cast <LinkGoTo *> (link_action);
+ link_dest = link_goto->getDest ();
+ named_dest = link_goto->getNamedDest ();
+
+ /* Wow! This seems excessively slow on large
+ * documents. I need to investigate more... -jrb */
+ if (link_dest != NULL) {
+ link_dest = link_dest->copy ();
+ } else if (named_dest != NULL) {
+ named_dest = named_dest->copy ();
+ link_dest = pdf_document->doc->findDest (named_dest);
+ delete named_dest;
+ }
+ if (link_dest != NULL) {
+ if (link_dest->isPageRef ()) {
+ page_ref = link_dest->getPageRef ();
+ page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
+ } else {
+ page_num = link_dest->getPageNum ();
+ }
+
+ delete link_dest;
+ }
+
+ break;
+ case actionURI:
+ link_uri = dynamic_cast <LinkURI *> (link_action);
+ break;
+
+ case actionNamed:
+ /*Skip, for now */
+ default:
+ g_warning ("Unknown link action type: %d", link_action->getKind ());
+ }
+
+ *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
+ } else if (link_title) {
+ *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
+ }
+
+ *type = EV_DOCUMENT_BOOKMARKS_TYPE_LINK;
+ *page = page_num;
+
+ return TRUE;
+}
+
+static EvDocumentBookmarksIter *
+pdf_document_bookmarks_get_child (EvDocumentBookmarks *document_bookmarks,
+ EvDocumentBookmarksIter *bookmarks_iter)
+{
+ BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
+ BookmarksIter *child_iter;
+ OutlineItem *anItem;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
+
+ anItem = (OutlineItem *)iter->items->get(iter->index);
+ anItem->open ();
+ if (! (anItem->hasKids() && anItem->getKids()) )
+ return NULL;
+
+ child_iter = g_new0 (BookmarksIter, 1);
+ child_iter->index = 0;
+ child_iter->level = iter->level + 1;
+ child_iter->items = anItem->getKids ();
+ g_assert (child_iter->items);
+
+ return (EvDocumentBookmarksIter *) child_iter;
+}
+
+static gboolean
+pdf_document_bookmarks_next (EvDocumentBookmarks *document_bookmarks,
+ EvDocumentBookmarksIter *bookmarks_iter)
+{
+ BookmarksIter *iter = (BookmarksIter *) bookmarks_iter;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
+
+ iter->index++;
+ if (iter->index >= iter->items->getLength())
+ return FALSE;