+static void
+pdf_document_search_emit_found (PdfDocumentSearch *search)
+{
+ PdfDocument *pdf_document = search->document;
+ int n_pages;
+ int pages_done;
+ GArray *tmp_results;
+ int i;
+
+ n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
+ if (search->search_page > search->start_page) {
+ pages_done = search->search_page - search->start_page;
+ } else if (search->search_page == search->start_page) {
+ pages_done = n_pages;
+ } else {
+ pages_done = n_pages - search->start_page + search->search_page;
+ }
+
+ tmp_results = g_array_new (FALSE, FALSE, sizeof (EvFindResult));
+ g_array_append_vals (tmp_results,
+ search->current_page_results->data,
+ search->current_page_results->len);
+
+ /* Now append a bogus element for each page that has a result in it,
+ * that is not the current page
+ */
+ i = 1;
+ while (i <= n_pages) {
+ if (i != pdf_document->page &&
+ search->other_page_flags[i]) {
+ EvFindResult result;
+
+ result.page_num = i;
+
+ /* Use bogus coordinates, again we can't get coordinates
+ * until this is the current page because TextOutputDev
+ * isn't good enough
+ */
+ result.highlight_area.x = -1;
+ result.highlight_area.y = -1;
+ result.highlight_area.width = 1;
+ result.highlight_area.height = 1;
+
+ g_array_append_val (tmp_results, result);
+ }
+
+ ++i;
+ }
+
+ ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
+ (EvFindResult*) tmp_results->data,
+ tmp_results->len,
+ pages_done / (double) n_pages);
+
+ g_array_free (tmp_results, TRUE);
+}
+
+static void
+pdf_document_search_page_changed (PdfDocumentSearch *search)
+{
+ PdfDocument *pdf_document = search->document;
+ int current_page;
+ EvFindResult result;
+ int xMin, yMin, xMax, yMax;
+
+ current_page = pdf_document->page;
+
+ if (!pdf_document->page_valid) {
+ /* we can't do anything until displayPage() */
+ search->current_page = -1;
+ return;
+ }
+
+ if (search->current_page == current_page)
+ return;
+
+ /* We need to create current_page_results for the new current page */
+ g_array_set_size (search->current_page_results, 0);
+
+ if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
+ gTrue, gTrue, // startAtTop, stopAtBottom
+ gFalse, gFalse, // startAtLast, stopAtLast
+ &xMin, &yMin, &xMax, &yMax)) {
+ result.page_num = pdf_document->page;
+
+ result.highlight_area.x = xMin;
+ result.highlight_area.y = yMin;
+ result.highlight_area.width = xMax - xMin;
+ result.highlight_area.height = yMax - yMin;
+
+ g_array_append_val (search->current_page_results, result);
+ /* Now find further results */
+
+ while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
+ gFalse, gTrue,
+ gTrue, gFalse,
+ &xMin, &yMin, &xMax, &yMax)) {
+
+ result.page_num = pdf_document->page;
+
+ result.highlight_area.x = xMin;
+ result.highlight_area.y = yMin;
+ result.highlight_area.width = xMax - xMin;
+ result.highlight_area.height = yMax - yMin;
+
+ g_array_append_val (search->current_page_results, result);
+ }
+ }
+
+ /* needed for the initial current page since we don't search
+ * it in the idle
+ */
+ search->other_page_flags[current_page] =
+ search->current_page_results->len > 0;
+
+ pdf_document_search_emit_found (search);
+}
+
+static gboolean
+pdf_document_search_idle_callback (void *data)
+{
+ PdfDocumentSearch *search = (PdfDocumentSearch*) data;
+ PdfDocument *pdf_document = search->document;
+ int n_pages;
+ double xMin, yMin, xMax, yMax;
+
+ /* Note that PDF page count is 1 through n_pages INCLUSIVE
+ * like a real book. We are looking to add one result for each
+ * page with a match, because the coordinates are meaningless
+ * with TextOutputDev, so we just want to flag matching pages
+ * and then when the user switches to the current page, we
+ * will emit "found" again with the real results.
+ */
+ n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
+
+ if (search->search_page == search->start_page) {
+ goto end_search;
+ }
+
+ if (search->output_dev == 0) {
+ /* First time through here... */
+ search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
+ if (!search->output_dev->isOk()) {
+ goto end_search;
+ }
+ }
+
+ pdf_document->doc->displayPage (search->output_dev,
+ search->search_page,
+ 72, 72, 0, gTrue, gFalse);
+
+ if (search->output_dev->findText (search->ucs4,
+ search->ucs4_len,
+ gTrue, gTrue, // startAtTop, stopAtBottom
+ gFalse, gFalse, // startAtLast, stopAtLast
+ &xMin, &yMin, &xMax, &yMax)) {
+ /* This page has results */
+ search->other_page_flags[search->search_page] = TRUE;
+ }
+
+ search->search_page += 1;
+ if (search->search_page > n_pages) {
+ /* wrap around */
+ search->search_page = 1;
+ }
+
+ /* We do this even if nothing was found, to update the percent complete */
+ pdf_document_search_emit_found (search);
+
+ return TRUE;
+
+ end_search:
+ /* We're done. */
+ search->idle = 0; /* will return FALSE to remove */
+ return FALSE;
+}
+
+static void
+pdf_document_find_begin (EvDocumentFind *document,
+ const char *search_string,
+ gboolean case_sensitive)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document);
+ PdfDocumentSearch *search;
+ int n_pages;
+ gunichar *ucs4;
+ glong ucs4_len;
+
+ /* FIXME handle case_sensitive (right now XPDF
+ * code is always case insensitive for ASCII
+ * and case sensitive for all other languaages)
+ */
+
+ 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 gboolean
+pdf_document_has_document_security (EvDocumentSecurity *document_security)
+{
+ /* FIXME: do we really need to have this? */
+ return FALSE;
+}
+
+static void
+pdf_document_set_password (EvDocumentSecurity *document_security,
+ const char *password)
+{
+ PdfDocument *document = PDF_DOCUMENT (document_security);
+
+ if (document->password)
+ g_free (document->password);
+
+ document->password = g_strdup (password);
+}
+
+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;
+}
+
+
+/* EvDocumentLinks Implementation */
+typedef struct
+{
+ /* goo GList, not glib */
+ GList *items;
+ int index;
+ int level;
+} LinksIter;
+
+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_links_has_document_links (EvDocumentLinks *document_links)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
+ Outline *outline;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
+
+ outline = pdf_document->doc->getOutline();
+ if (outline->getItems() != NULL &&
+ outline->getItems()->getLength() > 0)
+ return TRUE;
+
+ return FALSE;
+}
+
+static EvDocumentLinksIter *
+pdf_document_links_begin_read (EvDocumentLinks *document_links)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
+ Outline *outline;
+ LinksIter *iter;
+ GList *items;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
+
+ outline = pdf_document->doc->getOutline();
+ items = outline->getItems();
+ if (! items)
+ return NULL;
+
+ iter = g_new0 (LinksIter, 1);
+ iter->items = items;
+ iter->index = 0;
+ iter->level = 0;
+
+ return (EvDocumentLinksIter *) iter;
+}
+
+static EvLink *
+build_link_from_action (PdfDocument *pdf_document,
+ LinkAction *link_action,
+ const char *title)
+{
+ EvLink *link = NULL;
+
+ if (link_action == NULL) {
+ link = ev_link_new_title (title);
+ } else if (link_action->getKind () == actionGoTo) {
+ LinkDest *link_dest;
+ LinkGoTo *link_goto;
+ Ref page_ref;
+ gint page_num = 0;
+ GString *named_dest;
+
+ 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;
+ }
+
+ link = ev_link_new_page (title, page_num);
+ } else if (link_action->getKind () == actionURI) {
+ LinkURI *link_uri;
+
+ link_uri = dynamic_cast <LinkURI *> (link_action);
+ link = ev_link_new_external
+ (title, link_uri->getURI()->getCString());
+ } else if (link_action->getKind () == actionNamed) {
+ /*Skip, for now */
+ }
+
+ return link;
+}
+
+/* FIXME This returns a new object every time, probably we should cache it
+ in the iter */
+static EvLink *
+pdf_document_links_get_link (EvDocumentLinks *document_links,
+ EvDocumentLinksIter *links_iter)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
+ LinksIter *iter = (LinksIter *)links_iter;
+ OutlineItem *anItem;
+ LinkAction *link_action;
+ Unicode *link_title;
+ const char *title;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
+
+ anItem = (OutlineItem *)iter->items->get(iter->index);
+ link_action = anItem->getAction ();
+ link_title = anItem->getTitle ();
+ title = unicode_to_char (anItem, pdf_document->umap);
+
+ return build_link_from_action (pdf_document, link_action, title);
+}
+
+static EvDocumentLinksIter *
+pdf_document_links_get_child (EvDocumentLinks *document_links,
+ EvDocumentLinksIter *links_iter)
+{
+ LinksIter *iter = (LinksIter *)links_iter;
+ LinksIter *child_iter;
+ OutlineItem *anItem;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
+
+ anItem = (OutlineItem *)iter->items->get(iter->index);
+ anItem->open ();
+ if (! (anItem->hasKids() && anItem->getKids()) )
+ return NULL;
+
+ child_iter = g_new0 (LinksIter, 1);
+ child_iter->index = 0;
+ child_iter->level = iter->level + 1;
+ child_iter->items = anItem->getKids ();
+ g_assert (child_iter->items);
+
+ return (EvDocumentLinksIter *) child_iter;
+}
+
+static gboolean
+pdf_document_links_next (EvDocumentLinks *document_links,
+ EvDocumentLinksIter *links_iter)
+{
+ LinksIter *iter = (LinksIter *) links_iter;
+
+ g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
+
+ iter->index++;
+ if (iter->index >= iter->items->getLength())
+ return FALSE;
+
+ return TRUE;
+}
+
+static void
+pdf_document_links_free_iter (EvDocumentLinks *document_links,
+ EvDocumentLinksIter *iter)
+{
+ g_return_if_fail (PDF_IS_DOCUMENT (document_links));
+ g_return_if_fail (iter != NULL);
+
+ /* FIXME: Should I close all the nodes?? Free them? */
+ g_free (iter);
+}
+