Cleanup: halve calls to BLI_str_utf8_size_safe in the text editor

Syntax highlighted text was calculating the utf8 character
size twice per character.
This commit is contained in:
Campbell Barton 2022-02-04 14:00:13 +11:00
parent d730f79e0e
commit 7803312476
1 changed files with 16 additions and 15 deletions

View File

@ -79,10 +79,8 @@ static void text_font_end(const TextDrawContext *UNUSED(tdc))
static int text_font_draw(const TextDrawContext *tdc, int x, int y, const char *str)
{
int columns;
BLF_position(tdc->font_id, x, y, 0);
columns = BLF_draw_mono(tdc->font_id, str, BLF_DRAW_STR_DUMMY_MAX, tdc->cwidth_px);
const int columns = BLF_draw_mono(tdc->font_id, str, BLF_DRAW_STR_DUMMY_MAX, tdc->cwidth_px);
return tdc->cwidth_px * columns;
}
@ -95,13 +93,12 @@ static int text_font_draw_character(const TextDrawContext *tdc, int x, int y, ch
return tdc->cwidth_px;
}
static int text_font_draw_character_utf8(const TextDrawContext *tdc, int x, int y, const char *c)
static int text_font_draw_character_utf8(
const TextDrawContext *tdc, int x, int y, const char *c, const int c_len)
{
int columns;
const size_t len = BLI_str_utf8_size_safe(c);
BLI_assert(c_len == BLI_str_utf8_size_safe(c));
BLF_position(tdc->font_id, x, y, 0);
columns = BLF_draw_mono(tdc->font_id, c, len, tdc->cwidth_px);
const int columns = BLF_draw_mono(tdc->font_id, c, c_len, tdc->cwidth_px);
return tdc->cwidth_px * columns;
}
@ -463,13 +460,15 @@ static int text_draw_wrapped(const SpaceText *st,
}
/* Draw the visible portion of text on the overshot line */
for (a = fstart, ma = mstart; ma < mend; a++, ma += BLI_str_utf8_size_safe(str + ma)) {
for (a = fstart, ma = mstart; ma < mend; a++) {
if (use_syntax) {
if (fmt_prev != format[a]) {
format_draw_color(tdc, fmt_prev = format[a]);
}
}
x += text_font_draw_character_utf8(tdc, x, y, str + ma);
const int c_len = BLI_str_utf8_size_safe(str + ma);
x += text_font_draw_character_utf8(tdc, x, y, str + ma, c_len);
ma += c_len;
fpos++;
}
y -= TXT_LINE_HEIGHT(st);
@ -491,15 +490,16 @@ static int text_draw_wrapped(const SpaceText *st,
}
/* Draw the remaining text */
for (a = fstart, ma = mstart; str[ma] && y > clip_min_y;
a++, ma += BLI_str_utf8_size_safe(str + ma)) {
for (a = fstart, ma = mstart; str[ma] && y > clip_min_y; a++) {
if (use_syntax) {
if (fmt_prev != format[a]) {
format_draw_color(tdc, fmt_prev = format[a]);
}
}
x += text_font_draw_character_utf8(tdc, x, y, str + ma);
const int c_len = BLI_str_utf8_size_safe(str + ma);
x += text_font_draw_character_utf8(tdc, x, y, str + ma, c_len);
ma += c_len;
}
flatten_string_free(&fs);
@ -559,8 +559,9 @@ static void text_draw(const SpaceText *st,
if (format[a] != fmt_prev) {
format_draw_color(tdc, fmt_prev = format[a]);
}
x += text_font_draw_character_utf8(tdc, x, y, in + str_shift);
str_shift += BLI_str_utf8_size_safe(in + str_shift);
const int c_len = BLI_str_utf8_size_safe(in + str_shift);
x += text_font_draw_character_utf8(tdc, x, y, in + str_shift, c_len);
str_shift += c_len;
}
}
else {