Avoid drawing hidden text in `DocView:draw_line_text` (#1298)

* Stop drawing text past the `DocView` edge in `DocView:draw_line_text`

* Don't add draw commands if they fall outside the latest clip

The check was previously done with the window rect, so this will reduce 
a bit more the number of commands sent.
This commit is contained in:
Guldoman 2023-01-19 04:15:26 +01:00 committed by George Sokianos
parent 64065b98ca
commit 12bae1ec95
2 changed files with 9 additions and 3 deletions

View File

@ -422,6 +422,7 @@ function DocView:draw_line_text(line, x, y)
-- do not render newline, fixes issue #1164 -- do not render newline, fixes issue #1164
if tidx == last_token then text = text:sub(1, -2) end if tidx == last_token then text = text:sub(1, -2) end
tx = renderer.draw_text(font, text, tx, ty, color) tx = renderer.draw_text(font, text, tx, ty, color)
if tx > self.position.x + self.size.x then break end
end end
return self:get_line_height() return self:get_line_height()
end end

View File

@ -54,6 +54,7 @@ uint8_t *command_buf = NULL;
static bool resize_issue; static bool resize_issue;
static int command_buf_idx; static int command_buf_idx;
static RenRect screen_rect; static RenRect screen_rect;
static RenRect last_clip_rect;
static bool show_debug; static bool show_debug;
static inline int rencache_min(int a, int b) { return a < b ? a : b; } static inline int rencache_min(int a, int b) { return a < b ? a : b; }
@ -156,12 +157,15 @@ void rencache_show_debug(bool enable) {
void rencache_set_clip_rect(RenRect rect) { void rencache_set_clip_rect(RenRect rect) {
Command *cmd = push_command(SET_CLIP, COMMAND_BARE_SIZE); Command *cmd = push_command(SET_CLIP, COMMAND_BARE_SIZE);
if (cmd) { cmd->rect = intersect_rects(rect, screen_rect); } if (cmd) {
cmd->rect = intersect_rects(rect, screen_rect);
last_clip_rect = cmd->rect;
}
} }
void rencache_draw_rect(RenRect rect, RenColor color) { void rencache_draw_rect(RenRect rect, RenColor color) {
if (!rects_overlap(screen_rect, rect) || rect.width == 0 || rect.height == 0) { if (rect.width == 0 || rect.height == 0 || !rects_overlap(last_clip_rect, rect)) {
return; return;
} }
Command *cmd = push_command(DRAW_RECT, COMMAND_BARE_SIZE); Command *cmd = push_command(DRAW_RECT, COMMAND_BARE_SIZE);
@ -175,7 +179,7 @@ float rencache_draw_text(RenFont **fonts, const char *text, size_t len, float x,
{ {
float width = ren_font_group_get_width(fonts, text, len); float width = ren_font_group_get_width(fonts, text, len);
RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) }; RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) };
if (rects_overlap(screen_rect, rect)) { if (rects_overlap(last_clip_rect, rect)) {
int sz = len + 1; int sz = len + 1;
Command *cmd = push_command(DRAW_TEXT, COMMAND_BARE_SIZE + sz); Command *cmd = push_command(DRAW_TEXT, COMMAND_BARE_SIZE + sz);
if (cmd) { if (cmd) {
@ -207,6 +211,7 @@ void rencache_begin_frame() {
screen_rect.height = h; screen_rect.height = h;
rencache_invalidate(); rencache_invalidate();
} }
last_clip_rect = screen_rect;
} }