diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2012-10-31 19:10:06 +0100 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2012-11-16 14:05:07 +0100 |
commit | 433693b7cc813a7db2b1f8bbfe252550f6006ad2 (patch) | |
tree | fd8d33931faa85300e9829bc4cb4fe078fc5daec /fitz | |
parent | e50ede1410458db86c08d4368496b924e0d5d221 (diff) | |
download | mupdf-433693b7cc813a7db2b1f8bbfe252550f6006ad2.tar.xz |
Add functions to highlight and copy text selections.
Diffstat (limited to 'fitz')
-rw-r--r-- | fitz/doc_search.c | 105 | ||||
-rw-r--r-- | fitz/fitz.h | 14 |
2 files changed, 119 insertions, 0 deletions
diff --git a/fitz/doc_search.c b/fitz/doc_search.c index 88f1bf98..29a9de17 100644 --- a/fitz/doc_search.c +++ b/fitz/doc_search.c @@ -127,3 +127,108 @@ fz_search_text_page(fz_context *ctx, fz_text_page *text, char *needle, fz_bbox * return hit_count; } + +int +fz_highlight_selection(fz_context *ctx, fz_text_page *page, fz_bbox rect, fz_bbox *hit_bbox, int hit_max) +{ + fz_bbox linebox, charbox; + fz_text_block *block; + fz_text_line *line; + fz_text_span *span; + int i, hit_count; + + int x0 = rect.x0; + int x1 = rect.x1; + int y0 = rect.y0; + int y1 = rect.y1; + + hit_count = 0; + + for (block = page->blocks; block < page->blocks + page->len; block++) + { + for (line = block->lines; line < block->lines + block->len; line++) + { + linebox = fz_empty_bbox; + for (span = line->spans; span < line->spans + line->len; span++) + { + for (i = 0; i < span->len; i++) + { + charbox = fz_bbox_covering_rect(span->text[i].bbox); + if (charbox.x1 >= x0 && charbox.x0 <= x1 && charbox.y1 >= y0 && charbox.y0 <= y1) + { + if (charbox.y0 != linebox.y0 || fz_absi(charbox.x0 - linebox.x1) > 5) + { + if (!fz_is_empty_bbox(linebox) && hit_count < hit_max) + hit_bbox[hit_count++] = linebox; + linebox = charbox; + } + else + { + linebox = fz_union_bbox(linebox, charbox); + } + } + } + } + if (!fz_is_empty_bbox(linebox) && hit_count < hit_max) + hit_bbox[hit_count++] = linebox; + } + } + + return hit_count; +} + +char * +fz_copy_selection(fz_context *ctx, fz_text_page *page, fz_bbox rect) +{ + fz_buffer *buffer; + fz_bbox hitbox; + fz_text_block *block; + fz_text_line *line; + fz_text_span *span; + int c, i, seen = 0; + char *s; + + int x0 = rect.x0; + int x1 = rect.x1; + int y0 = rect.y0; + int y1 = rect.y1; + + buffer = fz_new_buffer(ctx, 1024); + + for (block = page->blocks; block < page->blocks + page->len; block++) + { + for (line = block->lines; line < block->lines + block->len; line++) + { + for (span = line->spans; span < line->spans + line->len; span++) + { + if (seen) + { + fz_write_buffer_byte(ctx, buffer, '\n'); + } + + seen = 0; + + for (i = 0; i < span->len; i++) + { + hitbox = fz_bbox_covering_rect(span->text[i].bbox); + c = span->text[i].c; + if (c < 32) + c = '?'; + if (hitbox.x1 >= x0 && hitbox.x0 <= x1 && hitbox.y1 >= y0 && hitbox.y0 <= y1) + { + fz_write_buffer_rune(ctx, buffer, c); + seen = 1; + } + } + + seen = (seen && span + 1 == line->spans + line->len); + } + } + } + + fz_write_buffer_byte(ctx, buffer, 0); + + s = (char*)buffer->data; + fz_free(ctx, buffer); + return s; +} diff --git a/fitz/fitz.h b/fitz/fitz.h index 2090b31f..f058df50 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -1804,6 +1804,20 @@ void fz_print_text_page(fz_context *ctx, FILE *out, fz_text_page *page); int fz_search_text_page(fz_context *ctx, fz_text_page *text, char *needle, fz_bbox *hit_bbox, int hit_max); /* + fz_highlight_selection: Return a list of rectangles to highlight given a selection rectangle. + + NOTE: This is an experimental interface and subject to change without notice. +*/ +int fz_highlight_selection(fz_context *ctx, fz_text_page *page, fz_bbox rect, fz_bbox *hit_bbox, int hit_max); + +/* + fz_copy_selection: Return a newly allocated UTF-8 string with the text for a given selection rectangle. + + NOTE: This is an experimental interface and subject to change without notice. +*/ +char *fz_copy_selection(fz_context *ctx, fz_text_page *page, fz_bbox rect); + +/* Cookie support - simple communication channel between app/library. */ |