From 12bae1ec955316f17e1582209241d94094bc0f84 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Thu, 19 Jan 2023 04:15:26 +0100 Subject: [PATCH] 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. --- data/core/docview.lua | 1 + src/rencache.c | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/data/core/docview.lua b/data/core/docview.lua index 6b7913c8..bf222738 100644 --- a/data/core/docview.lua +++ b/data/core/docview.lua @@ -422,6 +422,7 @@ function DocView:draw_line_text(line, x, y) -- do not render newline, fixes issue #1164 if tidx == last_token then text = text:sub(1, -2) end tx = renderer.draw_text(font, text, tx, ty, color) + if tx > self.position.x + self.size.x then break end end return self:get_line_height() end diff --git a/src/rencache.c b/src/rencache.c index 12c2a33c..f0243f0b 100644 --- a/src/rencache.c +++ b/src/rencache.c @@ -54,6 +54,7 @@ uint8_t *command_buf = NULL; static bool resize_issue; static int command_buf_idx; static RenRect screen_rect; +static RenRect last_clip_rect; static bool show_debug; 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) { 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) { - 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; } 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); 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; Command *cmd = push_command(DRAW_TEXT, COMMAND_BARE_SIZE + sz); if (cmd) { @@ -207,6 +211,7 @@ void rencache_begin_frame() { screen_rect.height = h; rencache_invalidate(); } + last_clip_rect = screen_rect; }