diff options
author | Vincent Sanders <vince@kyllikki.org> | 2020-05-23 18:39:25 +0100 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2020-05-23 23:00:08 +0100 |
commit | 36b9262e1481fc24d302b58f03006e733b2e6d16 (patch) | |
tree | a2189e438023c837d8b8949b7c38e54fa143cab1 /content/handlers/text | |
parent | 0432d9556187d00fd7f78963afcefc4b7dd71f2a (diff) | |
download | netsurf-36b9262e1481fc24d302b58f03006e733b2e6d16.tar.gz netsurf-36b9262e1481fc24d302b58f03006e733b2e6d16.tar.bz2 |
split selection redraw into content handler specific implementations
Diffstat (limited to 'content/handlers/text')
-rw-r--r-- | content/handlers/text/textplain.c | 136 | ||||
-rw-r--r-- | content/handlers/text/textplain.h | 13 |
2 files changed, 85 insertions, 64 deletions
diff --git a/content/handlers/text/textplain.c b/content/handlers/text/textplain.c index 534b91d93..d8f61be89 100644 --- a/content/handlers/text/textplain.c +++ b/content/handlers/text/textplain.c @@ -1452,6 +1452,68 @@ textplain_textsearch_find(struct content *c, /** + * Given a range of byte offsets within a UTF8 textplain content, + * return a box that fully encloses the text + * + * \param[in] c content of type CONTENT_TEXTPLAIN + * \param[in] start byte offset of start of text range + * \param[in] end byte offset of end + * \param[out] r rectangle to be completed + */ +static void +textplain_coords_from_range(struct content *c, + unsigned start, + unsigned end, + struct rect *r) +{ + textplain_content *text = (textplain_content *) c; + float line_height = textplain_line_height(); + char *utf8_data; + struct textplain_line *line; + unsigned lineno = 0; + unsigned nlines; + + assert(c != NULL); + assert(start <= end); + assert(end <= text->utf8_data_size); + + utf8_data = text->utf8_data; + nlines = text->physical_line_count; + line = text->physical_line; + + /* find start */ + lineno = textplain_find_line(c, start); + + r->y0 = (int)(MARGIN + lineno * line_height); + + if (lineno + 1 <= nlines || line[lineno + 1].start >= end) { + /* \todo - it may actually be more efficient just to + * run forwards most of the time + */ + + /* find end */ + lineno = textplain_find_line(c, end); + + r->x0 = 0; + r->x1 = text->formatted_width; + } else { + /* single line */ + const char *text = utf8_data + line[lineno].start; + + r->x0 = textplain_coord_from_offset(text, + start - line[lineno].start, + line[lineno].length); + + r->x1 = textplain_coord_from_offset(text, + end - line[lineno].start, + line[lineno].length); + } + + r->y1 = (int)(MARGIN + (lineno + 1) * line_height); +} + + +/** * get bounds of a free text search match */ static nserror @@ -1488,6 +1550,28 @@ textplain_create_selection(struct content *c, struct selection **sel_out) /** + * invalidate a region based on offsets into the text cauing a redraw + */ +static nserror +textplain_textselection_redraw(struct content *c, + unsigned start_idx, + unsigned end_idx) +{ + struct rect r; + + if (end_idx <= start_idx) { + return NSERROR_BAD_PARAMETER; + } + + textplain_coords_from_range(c, start_idx, end_idx, &r); + + content__request_redraw(c, r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); + + return NSERROR_OK; +} + + +/** * plain text content handler table */ static const content_handler textplain_content_handler = { @@ -1508,6 +1592,7 @@ static const content_handler textplain_content_handler = { .type = textplain_content_type, .textsearch_find = textplain_textsearch_find, .textsearch_bounds = textplain_textsearch_bounds, + .textselection_redraw = textplain_textselection_redraw, .create_selection = textplain_create_selection, .no_share = true, }; @@ -1555,58 +1640,7 @@ size_t textplain_size(struct content *c) } -/* exported interface documented in html/textplain.h */ -void -textplain_coords_from_range(struct content *c, - unsigned start, - unsigned end, - struct rect *r) -{ - textplain_content *text = (textplain_content *) c; - float line_height = textplain_line_height(); - char *utf8_data; - struct textplain_line *line; - unsigned lineno = 0; - unsigned nlines; - - assert(c != NULL); - assert(start <= end); - assert(end <= text->utf8_data_size); - - utf8_data = text->utf8_data; - nlines = text->physical_line_count; - line = text->physical_line; - /* find start */ - lineno = textplain_find_line(c, start); - - r->y0 = (int)(MARGIN + lineno * line_height); - - if (lineno + 1 <= nlines || line[lineno + 1].start >= end) { - /* \todo - it may actually be more efficient just to - * run forwards most of the time - */ - - /* find end */ - lineno = textplain_find_line(c, end); - - r->x0 = 0; - r->x1 = text->formatted_width; - } else { - /* single line */ - const char *text = utf8_data + line[lineno].start; - - r->x0 = textplain_coord_from_offset(text, - start - line[lineno].start, - line[lineno].length); - - r->x1 = textplain_coord_from_offset(text, - end - line[lineno].start, - line[lineno].length); - } - - r->y1 = (int)(MARGIN + (lineno + 1) * line_height); -} /* exported interface documented in html/textplain.h */ diff --git a/content/handlers/text/textplain.h b/content/handlers/text/textplain.h index 716397acc..206c5831e 100644 --- a/content/handlers/text/textplain.h +++ b/content/handlers/text/textplain.h @@ -47,19 +47,6 @@ size_t textplain_size(struct content *c); /** - * Given a range of byte offsets within a UTF8 textplain content, - * return a box that fully encloses the text - * - * \param[in] c content of type CONTENT_TEXTPLAIN - * \param[in] start byte offset of start of text range - * \param[in] end byte offset of end - * \param[out] r rectangle to be completed - */ -void textplain_coords_from_range(struct content *c, - unsigned start, unsigned end, struct rect *r); - - -/** * Return a pointer to the raw UTF-8 data, as opposed to the reformatted * text to fit the window width. Thus only hard newlines are preserved * in the saved/copied text of a selection. |