Author: tlsa
Date: Tue Mar 1 14:00:41 2011
New Revision: 11877
URL:
http://source.netsurf-browser.org?rev=11877&view=rev
Log:
Cache space widths.
Modified:
trunk/netsurf/desktop/textinput.c
trunk/netsurf/render/box.h
trunk/netsurf/render/box_construct.c
trunk/netsurf/render/html.h
trunk/netsurf/render/html_redraw.c
trunk/netsurf/render/layout.c
trunk/netsurf/render/textplain.c
Modified: trunk/netsurf/desktop/textinput.c
URL:
http://source.netsurf-browser.org/trunk/netsurf/desktop/textinput.c?rev=1...
==============================================================================
--- trunk/netsurf/desktop/textinput.c (original)
+++ trunk/netsurf/desktop/textinput.c Tue Mar 1 14:00:41 2011
@@ -44,7 +44,6 @@
#include "utils/talloc.h"
#include "utils/utf8.h"
#include "utils/utils.h"
-
/** ghost caret used to indicate the insertion point when dragging text
into a textarea/input field */
@@ -95,6 +94,8 @@
size_t *pchars);
static bool ensure_caret_visible(struct browser_window *bw,
struct box *textarea);
+
+#define SPACE_LEN(b) ((b->space == 0) ? 0 : 1)
/**
* Remove the given text caret from the window by invalidating it
@@ -424,11 +425,11 @@
selection_clear(bw->sel, false);
if (end_box != text_box ||
- char_offset < text_box->length + text_box->space) {
+ char_offset < text_box->length + SPACE_LEN(text_box)) {
/* there's something at the end of the line to delete */
- textarea_cut(bw, text_box, char_offset,
- end_box, end_box->length + end_box->space,
- false);
+ textarea_cut(bw, text_box, char_offset, end_box,
+ end_box->length + SPACE_LEN(end_box),
+ false);
reflow = true;
break;
}
@@ -753,11 +754,11 @@
if (reflow)
textarea_reflow(bw, textarea, inline_container);
- if (text_box->length + text_box->space <= char_offset) {
+ if (text_box->length + SPACE_LEN(text_box) <= char_offset) {
if (text_box->next && text_box->next->type == BOX_TEXT) {
/* the text box has been split when reflowing and
the caret is in the second part */
- char_offset -= (text_box->length + text_box->space);
+ char_offset -= (text_box->length + SPACE_LEN(text_box));
text_box = text_box->next;
assert(text_box);
assert(char_offset <= text_box->length);
@@ -771,7 +772,7 @@
(text_box->next &&
text_box->next->type == BOX_BR));
- char_offset = text_box->length + text_box->space;
+ char_offset = text_box->length + SPACE_LEN(text_box);
}
}
@@ -1376,13 +1377,13 @@
char_offset = textarea->gadget->caret_box_offset;
text_box = textarea->gadget->caret_text_box;
- while ((char_offset > text_box->length + text_box->space) &&
+ while ((char_offset > text_box->length + SPACE_LEN(text_box)) &&
(text_box->next) &&
(text_box->next->type == BOX_TEXT)) {
LOG(("Caret out of range: Was %d in boxlen %d "
"space %d", char_offset,
- text_box->length, text_box->space));
- char_offset -= text_box->length + text_box->space;
+ text_box->length, SPACE_LEN(text_box)));
+ char_offset -= text_box->length + SPACE_LEN(text_box);
text_box = text_box->next;
}
@@ -1716,15 +1717,15 @@
/* insert in text box */
text = talloc_realloc(current_content, text_box->text,
char,
- text_box->length + text_box->space + utf8_len + 1);
+ text_box->length + SPACE_LEN(text_box) + utf8_len + 1);
if (!text) {
warn_user("NoMemory", 0);
return false;
}
text_box->text = text;
- if (text_box->space &&
- char_offset == text_box->length + text_box->space) {
+ if (text_box->space != 0 &&
+ char_offset == text_box->length + SPACE_LEN(text_box)) {
if (hide)
text_box->space = 0;
else {
@@ -1798,18 +1799,18 @@
}
/* delete from visible textbox */
- if (next_offset <= text_box->length + text_box->space) {
+ if (next_offset <= text_box->length + SPACE_LEN(text_box)) {
/* handle removal of trailing space */
- if (text_box->space && next_offset > text_box->length) {
+ if (text_box->space != 0 && next_offset > text_box->length) {
if (char_offset > 0) {
/* is the trailing character still a space? */
int tmp = utf8_prev(text_box->text, char_offset);
if (isspace(text_box->text[tmp]))
char_offset = tmp;
else
- text_box->space = false;
+ text_box->space = 0;
} else {
- text_box->space = false;
+ text_box->space = 0;
}
text_box->length = char_offset;
@@ -1846,7 +1847,7 @@
bool delete_handler(struct browser_window *bw, struct box *b,
int offset, size_t length)
{
- size_t text_length = b->length + b->space;
+ size_t text_length = b->length + SPACE_LEN(b);
/* only remove if its not the first box */
if (offset <= 0 && length >= text_length && b->prev != NULL) {
@@ -2053,21 +2054,23 @@
/* append box text to clipboard and then delete it */
if (clipboard &&
!gui_add_to_clipboard(box->text + start_idx,
- box->length - start_idx, box->space)) {
+ box->length - start_idx,
+ SPACE_LEN(box))) {
gui_commit_clipboard();
return false;
}
if (del) {
if (!delete_handler(bw, box, start_idx,
- (box->length + box->space) -
+ (box->length + SPACE_LEN(box)) -
start_idx) && clipboard) {
gui_commit_clipboard();
return false;
}
} else {
textbox_delete(bw, box, start_idx,
- (box->length + box->space) - start_idx);
+ (box->length + SPACE_LEN(box)) -
+ start_idx);
}
}
Modified: trunk/netsurf/render/box.h
URL:
http://source.netsurf-browser.org/trunk/netsurf/render/box.h?rev=11877&am...
==============================================================================
--- trunk/netsurf/render/box.h (original)
+++ trunk/netsurf/render/box.h Tue Mar 1 14:00:41 2011
@@ -99,6 +99,10 @@
struct column;
struct object_params;
struct object_param;
+
+
+#define UNKNOWN_WIDTH INT_MAX
+#define UNKNOWN_MAX_WIDTH INT_MAX
/** Type of a struct box. */
@@ -187,8 +191,8 @@
char *text; /**< Text, or 0 if none. Unterminated. */
size_t length; /**< Length of text. */
- /** Text is followed by a space. */
- unsigned int space : 1;
+ /** Width of space after current text (depends on font and size). */
+ int space;
/** This box is a continuation of the previous box (eg from line
* breaking). */
unsigned int clone : 1;
@@ -293,8 +297,6 @@
extern const char *TARGET_BLANK;
-#define UNKNOWN_WIDTH INT_MAX
-#define UNKNOWN_MAX_WIDTH INT_MAX
void *box_style_alloc(void *ptr, size_t len, void *pw);
struct box * box_create(css_select_results *styles, css_computed_style *style,
Modified: trunk/netsurf/render/box_construct.c
URL:
http://source.netsurf-browser.org/trunk/netsurf/render/box_construct.c?re...
==============================================================================
--- trunk/netsurf/render/box_construct.c (original)
+++ trunk/netsurf/render/box_construct.c Tue Mar 1 14:00:41 2011
@@ -718,7 +718,8 @@
assert((*inline_container)->last != 0);
- (*inline_container)->last->space = 1;
+ (*inline_container)->last->space =
+ UNKNOWN_WIDTH;
}
free(text);
@@ -759,7 +760,7 @@
/* strip ending space char off */
if (box->length > 1 && box->text[box->length - 1] == ' ')
{
- box->space = 1;
+ box->space = UNKNOWN_WIDTH;
box->length--;
}
@@ -797,7 +798,7 @@
memmove(box->text, &box->text[1], box->length);
if (box->prev != NULL)
- box->prev->space = 1;
+ box->prev->space = UNKNOWN_WIDTH;
}
} else {
Modified: trunk/netsurf/render/html.h
URL:
http://source.netsurf-browser.org/trunk/netsurf/render/html.h?rev=11877&a...
==============================================================================
--- trunk/netsurf/render/html.h (original)
+++ trunk/netsurf/render/html.h Tue Mar 1 14:00:41 2011
@@ -225,7 +225,7 @@
browser_mouse_state mouse, int x, int y, int dir);
bool text_redraw(const char *utf8_text, size_t utf8_len,
- size_t offset, bool space,
+ size_t offset, int space,
const plot_font_style_t *fstyle,
int x, int y,
const struct rect *clip,
Modified: trunk/netsurf/render/html_redraw.c
URL:
http://source.netsurf-browser.org/trunk/netsurf/render/html_redraw.c?rev=...
==============================================================================
--- trunk/netsurf/render/html_redraw.c (original)
+++ trunk/netsurf/render/html_redraw.c Tue Mar 1 14:00:41 2011
@@ -820,7 +820,7 @@
* \param utf8_text pointer to UTF-8 text string
* \param utf8_len length of string, in bytes
* \param offset byte offset within textual representation
- * \param space indicates whether string is followed by a space
+ * \param space width of space that follows string (0 = no space)
* \param fstyle text style to use
* \param x x ordinate at which to plot text
* \param y y ordinate at which to plot text
@@ -832,7 +832,7 @@
*/
bool text_redraw(const char *utf8_text, size_t utf8_len,
- size_t offset, bool space, const plot_font_style_t *fstyle,
+ size_t offset, int space, const plot_font_style_t *fstyle,
int x, int y, const struct rect *clip, int height,
float scale, bool excluded)
{
@@ -891,12 +891,7 @@
/* is there a trailing space that should be highlighted
* as well? */
if (end_idx > utf8_len) {
- int spc_width;
- /* \todo is there a more elegant/efficient
- * solution? */
- if (nsfont.font_width(fstyle, " ", 1,
- &spc_width))
- endx += spc_width;
+ endx += space;
}
if (scale != 1.0) {
Modified: trunk/netsurf/render/layout.c
URL:
http://source.netsurf-browser.org/trunk/netsurf/render/layout.c?rev=11877...
==============================================================================
--- trunk/netsurf/render/layout.c (original)
+++ trunk/netsurf/render/layout.c Tue Mar 1 14:00:41 2011
@@ -1922,6 +1922,11 @@
struct box *c2;
const struct font_functions *font_func = content->data.html.font_func;
+ if (split_box->space == 0 || split_box->space == UNKNOWN_WIDTH) {
+ font_func->font_width(fstyle, " ", 1, &split_box->space);
+ }
+ space_width = split_box->space;
+
/* Create clone of split_box, c2 */
c2 = talloc_memdup(content, split_box, sizeof *c2);
if (!c2)
@@ -1935,14 +1940,12 @@
return false;
/* Set c2 according to the remaining text */
- font_func->font_width(fstyle, " ", 1, &space_width);
c2->width -= new_width + space_width;
c2->length = split_box->length - (new_length + 1);
/* Update split_box for its reduced text */
split_box->length = new_length;
split_box->width = new_width;
- split_box->space = 1;
/* Insert c2 into box list */
c2->next = split_box->next;
@@ -2100,13 +2103,13 @@
}
} else if (b->type == BOX_INLINE_END) {
b->width = 0;
- if (b->space) {
- /** \todo optimize out */
+ if (b->space == UNKNOWN_WIDTH) {
font_func->font_width(&fstyle, " ", 1,
- &space_after);
- } else {
- space_after = 0;
- }
+ &b->space);
+ /** \todo handle errors */
+ }
+ space_after = b->space;
+
x += b->padding[RIGHT] + b->border[RIGHT].width +
b->margin[RIGHT];
continue;
@@ -2153,18 +2156,17 @@
b->width += SCROLLBAR_WIDTH;
} else {
font_func->font_width(&fstyle, b->text,
- b->length, &b->width);
+ b->length, &b->width);
}
}
x += b->width;
- if (b->space)
- /** \todo optimize out */
+ if (b->space == UNKNOWN_WIDTH) {
font_func->font_width(&fstyle, " ", 1,
- &space_after);
- else
- space_after = 0;
-
+ &b->space);
+ /** \todo handle errors */
+ }
+ space_after = b->space;
continue;
}
@@ -2285,16 +2287,17 @@
if (b->object)
space_after = 0;
else if (b->text || b->type == BOX_INLINE_END) {
- space_after = 0;
- if (b->space) {
+ if (b->space == UNKNOWN_WIDTH) {
font_plot_style_from_css(b->style,
&fstyle);
- /** \todo handle errors, optimize */
+ /** \todo handle errors */
font_func->font_width(&fstyle, " ", 1,
- &space_after);
+ &b->space);
}
- } else
+ space_after = b->space;
+ } else {
space_after = 0;
+ }
split_box = b;
move_y = true;
inline_count++;
@@ -2726,9 +2729,10 @@
&fixed, &frac);
if (0 < fixed)
max += fixed;
- if (b->next && b->space) {
- font_func->font_width(&fstyle, " ", 1, &width);
- max += width;
+ if (b->next && b->space == UNKNOWN_WIDTH) {
+ font_func->font_width(&fstyle, " ", 1,
+ &b->space);
+ max += b->space;
}
continue;
}
@@ -2772,9 +2776,10 @@
}
}
max += b->width;
- if (b->next && b->space) {
- font_func->font_width(&fstyle, " ", 1, &width);
- max += width;
+ if (b->next && b->space == UNKNOWN_WIDTH) {
+ font_func->font_width(&fstyle, " ", 1,
+ &b->space);
+ max += b->space;
}
/* min = widest word */
Modified: trunk/netsurf/render/textplain.c
URL:
http://source.netsurf-browser.org/trunk/netsurf/render/textplain.c?rev=11...
==============================================================================
--- trunk/netsurf/render/textplain.c (original)
+++ trunk/netsurf/render/textplain.c Tue Mar 1 14:00:41 2011
@@ -612,7 +612,7 @@
next_offset = utf8_next(text, length, next_offset);
if (!text_redraw(text + offset, next_offset - offset,
- line[lineno].start + offset, false,
+ line[lineno].start + offset, 0,
&textplain_style,
tx, y + (lineno * scaled_line_height),
clip, line_height, scale, false))