X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=pdf%2Fxpdf%2Fpdf-document.cc;h=82838f471624a75916a95dd0766d259f9890067f;hb=662b57ab5fc05d98a0ab3e4258c15cf2a2cc66c1;hp=b9180bd9988b1e712c4c79a445401f5777e33e3e;hpb=16291d257b4e25d4d49588ae0c4f309e6fe81cd1;p=evince.git diff --git a/pdf/xpdf/pdf-document.cc b/pdf/xpdf/pdf-document.cc index b9180bd9..82838f47 100644 --- a/pdf/xpdf/pdf-document.cc +++ b/pdf/xpdf/pdf-document.cc @@ -23,9 +23,9 @@ #include "pdf-document.h" #include "ev-ps-exporter.h" #include "ev-document-find.h" +#include "ev-document-misc.h" #include "gpdf-g-switch.h" #include "ev-document-links.h" -#include "ev-document-misc.h" #include "ev-document-security.h" #include "ev-document-thumbnails.h" @@ -56,7 +56,7 @@ typedef struct /* full results are only possible for the rendered current page */ int current_page; GArray *current_page_results; - guchar *other_page_flags; /* length n_pages + 1, first element ignored */ + int *other_page_flags; /* length n_pages + 1, first element ignored */ int start_page; /* skip this one as we iterate, since we did it first */ int search_page; /* the page we're searching now */ TextOutputDev *output_dev; @@ -90,7 +90,6 @@ struct _PdfDocument UnicodeMap *umap; gchar *password; - gboolean page_valid; PdfDocumentSearch *search; }; @@ -136,29 +135,21 @@ document_init_links (PdfDocument *pdf_document) obj.free (); } -static gboolean -document_validate_page (PdfDocument *pdf_document) +static void +document_display_page (PdfDocument *pdf_document) { - if (!pdf_document->page_valid) { - pdf_document->doc->displayPage (pdf_document->out, pdf_document->page, - 72 * pdf_document->scale, - 72 * pdf_document->scale, - 0, gTrue, gTrue); - - document_init_links (pdf_document); + pdf_document->doc->displayPage (pdf_document->out, pdf_document->page, + 72 * pdf_document->scale, + 72 * pdf_document->scale, + 0, gTrue, gTrue); - pdf_document->page_valid = TRUE; + document_init_links (pdf_document); - ev_document_changed (EV_DOCUMENT (pdf_document)); - - /* Update the search results available to the app since - * we only provide full results on the current page - */ - if (pdf_document->search) - pdf_document_search_page_changed (pdf_document->search); - } - - return pdf_document->page_valid; + /* Update the search results available to the app since + * we only provide full results on the current page + */ + if (pdf_document->search) + pdf_document_search_page_changed (pdf_document->search); } static gboolean @@ -227,8 +218,6 @@ pdf_document_load (EvDocument *document, if (pdf_document->out) pdf_document->out->startDoc(pdf_document->doc->getXRef()); - pdf_document->page_valid = FALSE; - g_object_notify (G_OBJECT (pdf_document), "title"); return TRUE; @@ -274,7 +263,8 @@ pdf_document_set_page (EvDocument *document, if (page != pdf_document->page) { pdf_document->page = page; - pdf_document->page_valid = FALSE; + document_display_page (pdf_document); + ev_document_page_changed (EV_DOCUMENT (pdf_document)); } } @@ -321,7 +311,7 @@ pdf_document_set_target (EvDocument *document, } - pdf_document->page_valid = FALSE; + /* FIXME we need to regenerate pages */ } } @@ -333,7 +323,8 @@ pdf_document_set_scale (EvDocument *document, if (pdf_document->scale != scale) { pdf_document->scale = scale; - pdf_document->page_valid = FALSE; + document_display_page (pdf_document); + ev_document_scale_changed (EV_DOCUMENT (pdf_document)); } } @@ -350,21 +341,26 @@ pdf_document_set_page_offset (EvDocument *document, static void pdf_document_get_page_size (EvDocument *document, + int page, int *width, int *height) { PdfDocument *pdf_document = PDF_DOCUMENT (document); + Page *the_page; - if (document_validate_page (pdf_document)) { - if (width) - *width = pdf_document->out->getBitmapWidth(); - if (height) - *height = pdf_document->out->getBitmapHeight(); - } else { - if (width) - *width = 1; - if (height) - *height = 1; + /* set some default values */ + if (width) + *width = 1; + if (height) + *height = 1; + + if (page == -1) + page = pdf_document->page; + + the_page = pdf_document->doc->getCatalog ()->getPage (page); + if (the_page) { + *width = (int) ((the_page->getWidth () * pdf_document->scale) + 0.5); + *height = (int) ((the_page->getHeight () * pdf_document->scale) + 0.5); } } @@ -379,7 +375,7 @@ pdf_document_render (EvDocument *document, GdkRectangle page; GdkRectangle draw; - if (!document_validate_page (pdf_document) || !pdf_document->target) + if (!pdf_document->target) return; page.x = pdf_document->page_x_offset; @@ -399,61 +395,76 @@ pdf_document_render (EvDocument *document, draw.width, draw.height); } -static void -pdf_document_search_emit_found (PdfDocumentSearch *search) +double +pdf_document_find_get_progress (EvDocumentFind *document_find) { - PdfDocument *pdf_document = search->document; - int n_pages; - int pages_done; - GArray *tmp_results; - int i; + PdfDocumentSearch *search; + int n_pages, pages_done; - 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; - } + search = PDF_DOCUMENT (document_find)->search; - 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); + if (search == NULL) { + return 0; + } - /* 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); - } + n_pages = ev_document_get_n_pages (EV_DOCUMENT (document_find)); + if (search->search_page > search->start_page) { + pages_done = search->search_page - search->start_page + 1; + } else if (search->search_page == search->start_page) { + pages_done = n_pages; + } else { + pages_done = n_pages - search->start_page + search->search_page; + } - ++i; - } + return pages_done / (double) n_pages; +} + +int +pdf_document_find_page_has_results (EvDocumentFind *document_find, + int page) +{ + PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search; + + g_return_val_if_fail (search != NULL, FALSE); + + return search->other_page_flags[page]; +} + +int +pdf_document_find_get_n_results (EvDocumentFind *document_find) +{ + PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search; + + if (search) { + return search->current_page_results->len; + } else { + return 0; + } +} - ev_document_find_found (EV_DOCUMENT_FIND (pdf_document), - (EvFindResult*) tmp_results->data, - tmp_results->len, - pages_done / (double) n_pages); +gboolean +pdf_document_find_get_result (EvDocumentFind *document_find, + int n_result, + GdkRectangle *rectangle) +{ + PdfDocument *pdf_document = PDF_DOCUMENT (document_find); + PdfDocumentSearch *search = pdf_document->search; + GdkRectangle r; + + if (search != NULL && + n_result < search->current_page_results->len) { + r = g_array_index (search->current_page_results, + GdkRectangle, n_result); + + rectangle->x = r.x + pdf_document->page_x_offset; + rectangle->y = r.y + pdf_document->page_y_offset; + rectangle->width = r.width; + rectangle->height = r.height; - g_array_free (tmp_results, TRUE); + return TRUE; + } else { + return FALSE; + } } static void @@ -461,17 +472,11 @@ pdf_document_search_page_changed (PdfDocumentSearch *search) { PdfDocument *pdf_document = search->document; int current_page; - EvFindResult result; + GdkRectangle 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; @@ -482,12 +487,10 @@ pdf_document_search_page_changed (PdfDocumentSearch *search) gTrue, gTrue, // startAtTop, stopAtBottom gFalse, gFalse, // startAtLast, stopAtLast &xMin, &yMin, &xMax, &yMax)) { - result.page_num = pdf_document->page; - - result.highlight_area.x = xMin + pdf_document->page_x_offset; - result.highlight_area.y = yMin + pdf_document->page_y_offset; - result.highlight_area.width = xMax - xMin; - result.highlight_area.height = yMax - yMin; + result.x = xMin; + result.y = yMin; + result.width = xMax - xMin; + result.height = yMax - yMin; g_array_append_val (search->current_page_results, result); /* Now find further results */ @@ -496,25 +499,14 @@ pdf_document_search_page_changed (PdfDocumentSearch *search) gFalse, gTrue, gTrue, gFalse, &xMin, &yMin, &xMax, &yMax)) { - - result.page_num = pdf_document->page; - - result.highlight_area.x = xMin + pdf_document->page_x_offset; - result.highlight_area.y = yMin + pdf_document->page_y_offset; - result.highlight_area.width = xMax - xMin; - result.highlight_area.height = yMax - yMin; + result.x = xMin; + result.y = yMin; + result.width = xMax - xMin; + result.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 @@ -522,7 +514,7 @@ pdf_document_search_idle_callback (void *data) { PdfDocumentSearch *search = (PdfDocumentSearch*) data; PdfDocument *pdf_document = search->document; - int n_pages; + int n_pages, changed_page; double xMin, yMin, xMax, yMax; /* Note that PDF page count is 1 through n_pages INCLUSIVE @@ -534,10 +526,6 @@ pdf_document_search_idle_callback (void *data) */ 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); @@ -556,8 +544,12 @@ pdf_document_search_idle_callback (void *data) gFalse, gFalse, // startAtLast, stopAtLast &xMin, &yMin, &xMax, &yMax)) { /* This page has results */ - search->other_page_flags[search->search_page] = TRUE; - } + search->other_page_flags[search->search_page] = 1; + } else { + search->other_page_flags[search->search_page] = 0; + } + + changed_page = search->start_page; search->search_page += 1; if (search->search_page > n_pages) { @@ -565,12 +557,13 @@ pdf_document_search_idle_callback (void *data) 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; + if (search->search_page != search->start_page) { + ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document), + changed_page); + return TRUE; + } - end_search: +end_search: /* We're done. */ search->idle = 0; /* will return FALSE to remove */ return FALSE; @@ -583,7 +576,7 @@ pdf_document_find_begin (EvDocumentFind *document, { PdfDocument *pdf_document = PDF_DOCUMENT (document); PdfDocumentSearch *search; - int n_pages; + int n_pages, i; gunichar *ucs4; glong ucs4_len; @@ -618,13 +611,13 @@ pdf_document_find_begin (EvDocumentFind *document, search->current_page_results = g_array_new (FALSE, FALSE, - sizeof (EvFindResult)); + sizeof (GdkRectangle)); 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->other_page_flags = g_new0 (int, n_pages + 1); + for (i = 0; i <= n_pages; i++) { + search->other_page_flags[i] = -1; + } search->document = pdf_document; @@ -637,9 +630,7 @@ pdf_document_find_begin (EvDocumentFind *document, 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->search_page = search->start_page; search->current_page = -1; @@ -650,7 +641,7 @@ pdf_document_find_begin (EvDocumentFind *document, } static void -pdf_document_find_cancel (EvDocumentFind *document) +pdf_document_find_cancel (EvDocumentFind *document) { PdfDocument *pdf_document = PDF_DOCUMENT (document); @@ -802,6 +793,14 @@ build_link_from_action (PdfDocument *pdf_document, if (link_action == NULL) { link = ev_link_new_title (title); + } else if (link_action->getKind () == actionGoToR) { + g_warning ("actionGoToR links not implemented"); + } else if (link_action->getKind () == actionLaunch) { + g_warning ("actionLaunch links not implemented"); + } else if (link_action->getKind () == actionNamed) { + g_warning ("actionNamed links not implemented"); + } else if (link_action->getKind () == actionMovie) { + g_warning ("actionMovie links not implemented"); } else if (link_action->getKind () == actionGoTo) { LinkDest *link_dest; LinkGoTo *link_goto; @@ -839,8 +838,13 @@ build_link_from_action (PdfDocument *pdf_document, link_uri = dynamic_cast (link_action); link = ev_link_new_external (title, link_uri->getURI()->getCString()); - } else if (link_action->getKind () == actionNamed) { - /*Skip, for now */ + } else if (link_action->getKind () == actionUnknown) { + LinkUnknown *link_unknown; + + link_unknown = dynamic_cast (link_action); + + g_warning ("Unknown link type %s", + link_unknown->getAction()->getCString()); } return link; @@ -1006,6 +1010,8 @@ pdf_document_get_title (PdfDocument *pdf_document) char *title = NULL; Object info; + if (pdf_document->doc == NULL) + return NULL; pdf_document->doc->getDocInfo (&info); if (info.isDict ()) { @@ -1023,10 +1029,10 @@ pdf_document_get_text (EvDocument *document, GdkRectangle *rect) const char *text; int x1, y1, x2, y2; - x1 = rect->x + pdf_document->page_x_offset; - y1 = rect->y + pdf_document->page_y_offset; - x2 = x1 + rect->width + pdf_document->page_x_offset; - y2 = y1 + rect->height + pdf_document->page_y_offset; + x1 = rect->x - pdf_document->page_x_offset; + y1 = rect->y - pdf_document->page_y_offset; + x2 = x1 + rect->width; + y2 = y1 + rect->height; sel_text = pdf_document->out->getText (x1, y1, x2, y2); text = sel_text->getCString (); @@ -1041,17 +1047,21 @@ pdf_document_get_link (EvDocument *document, int x, int y) LinkAction *action; double link_x, link_y; - /* Zoom */ - link_x = x / pdf_document->scale; - link_y = y / pdf_document->scale; + if (pdf_document->links == NULL) { + return NULL; + } /* Offset */ - link_x -= pdf_document->page_x_offset; - link_y -= pdf_document->page_y_offset; + link_x = x - pdf_document->page_x_offset; + link_y = y - pdf_document->page_y_offset; /* Inverse y */ link_y = pdf_document->out->getBitmapHeight() - link_y; + /* Zoom */ + link_x = link_x / pdf_document->scale; + link_y = link_y / pdf_document->scale; + action = pdf_document->links->find (link_x, link_y); if (action) { @@ -1122,6 +1132,10 @@ static void pdf_document_find_iface_init (EvDocumentFindIface *iface) { iface->begin = pdf_document_find_begin; + iface->get_n_results = pdf_document_find_get_n_results; + iface->get_result = pdf_document_find_get_result; + iface->page_has_results = pdf_document_find_page_has_results; + iface->get_progress = pdf_document_find_get_progress; iface->cancel = pdf_document_find_cancel; } @@ -1241,8 +1255,7 @@ pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnail Thumb *thumb = NULL; gdouble page_ratio; - /* getPage seems to want page + 1 for some reason; */ - the_page = pdf_document->doc->getCatalog ()->getPage (page + 1); + the_page = pdf_document->doc->getCatalog ()->getPage (page); the_page->getThumb (&the_thumb); if (!(the_thumb.isNull () || the_thumb.isNone())) { @@ -1335,7 +1348,6 @@ pdf_document_init (PdfDocument *pdf_document) pdf_document->page_y_offset = 0; pdf_document->scale = 1.; - pdf_document->page_valid = FALSE; pdf_document->password = NULL; }