diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2011-11-14 20:24:54 +0100 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2011-11-22 16:52:38 +0100 |
commit | 7b0e83ea2e177f30bd2f9a17894217066deb7ea5 (patch) | |
tree | 1926784cde941a06ddda8785a313057832cfbacc | |
parent | 3f608056b53cf55ed76983d92c32e5a94da1427e (diff) | |
download | mupdf-7b0e83ea2e177f30bd2f9a17894217066deb7ea5.tar.xz |
Add searching to the high level document API in the iOS app.
-rw-r--r-- | ios/document.c | 91 | ||||
-rw-r--r-- | ios/document.h | 1 |
2 files changed, 92 insertions, 0 deletions
diff --git a/ios/document.c b/ios/document.c index 6d30a0bc..b48e0d33 100644 --- a/ios/document.c +++ b/ios/document.c @@ -3,6 +3,8 @@ #include "xps/muxps.h" #include "document.h" +#include <ctype.h> // for tolower() + struct document * open_document(char *filename) { @@ -137,6 +139,95 @@ draw_page(struct document *doc, int number, fz_device *dev, fz_matrix ctm) fz_flush_warnings(); } +static int +charat(fz_text_span *span, int idx) +{ + int ofs = 0; + while (span) { + if (idx < ofs + span->len) + return span->text[idx - ofs].c; + if (span->eol) { + if (idx == ofs + span->len) + return ' '; + ofs ++; + } + ofs += span->len; + span = span->next; + } + return 0; +} + +static fz_bbox +bboxat(fz_text_span *span, int idx) +{ + int ofs = 0; + while (span) { + if (idx < ofs + span->len) + return span->text[idx - ofs].bbox; + if (span->eol) { + if (idx == ofs + span->len) + return fz_empty_bbox; + ofs ++; + } + ofs += span->len; + span = span->next; + } + return fz_empty_bbox; +} + +static int +textlen(fz_text_span *span) +{ + int len = 0; + while (span) { + len += span->len; + if (span->eol) + len ++; + span = span->next; + } + return len; +} + +static int +match(fz_text_span *span, char *s, int n) +{ + int start = n, c; + while ((c = *s++)) { + if (c == ' ' && charat(span, n) == ' ') { + while (charat(span, n) == ' ') + n++; + } else { + if (tolower(c) != tolower(charat(span, n))) + return 0; + n++; + } + } + return n - start; +} + +int +search_page(struct document *doc, int number, char *needle) +{ + int pos, len, count = 0; + fz_text_span *text = fz_new_text_span(); + fz_device *dev = fz_new_text_device(text); + draw_page(doc, number, dev, fz_identity); + fz_free_device(dev); + + len = textlen(text); + for (pos = 0; pos < len; pos++) { + int n = match(text, needle, pos); + if (n) { + // TODO: extract bbox(es) into a result list + printf("found a match at page %d, pos %d!\n", number, pos); + count++; + } + } + + fz_free_text_span(text); + return count; +} + void close_document(struct document *doc) { diff --git a/ios/document.h b/ios/document.h index 1f59ccdd..00805a5d 100644 --- a/ios/document.h +++ b/ios/document.h @@ -27,6 +27,7 @@ fz_outline *load_outline(struct document *doc); int count_pages(struct document *doc); void measure_page(struct document *doc, int number, float *w, float *h); void draw_page(struct document *doc, int number, fz_device *dev, fz_matrix ctm); +int search_page(struct document *doc, int number, char *needle); void close_document(struct document *doc); #endif |