From 72e5b85338b29a93898c5d917c5186ac1c162dbc Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Mon, 12 Jul 2021 22:24:21 +0700 Subject: [PATCH] Draw wide characters correctly Draw the full background for wide characters as soon as they are encountered. Don't draw any foreground or background if width is zero as this means the previous character was wide. Fixes #17 --- addons/godot_xterm/native/src/terminal.cpp | 15 +++++++++------ addons/godot_xterm/native/src/terminal.h | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index 65ee90e..4509255 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -253,15 +253,18 @@ static int text_draw_cb(struct tsm_screen *con, uint64_t id, const uint32_t *ch, age <= terminal->framebuffer_age) return 0; - std::pair color_pair = terminal->get_cell_colors(attr); - terminal->draw_background(row, col, color_pair.first); + if (width < 1) // No foreground or background to draw. + return 0; - size_t ulen; - char buf[5] = {0}; + std::pair color_pair = terminal->get_cell_colors(attr); + terminal->draw_background(row, col, color_pair.first, width); if (len < 1) // No foreground to draw. return 0; + size_t ulen; + char buf[5] = {0}; + char *utf8 = tsm_ucs4_to_utf8_alloc(ch, len, &ulen); memcpy(buf, utf8, ulen); terminal->draw_foreground(row, col, buf, attr, color_pair.second); @@ -473,10 +476,10 @@ void Terminal::update_theme() { update_size(); } -void Terminal::draw_background(int row, int col, Color bgcolor) { +void Terminal::draw_background(int row, int col, Color bgcolor, int width = 1) { /* Draw the background */ Vector2 background_pos = Vector2(col * cell_size.x, row * cell_size.y); - Rect2 background_rect = Rect2(background_pos, cell_size); + Rect2 background_rect = Rect2(background_pos, cell_size * Vector2(width, 1)); draw_rect(background_rect, bgcolor); } diff --git a/addons/godot_xterm/native/src/terminal.h b/addons/godot_xterm/native/src/terminal.h index edaaa8d..1c3a7ec 100644 --- a/addons/godot_xterm/native/src/terminal.h +++ b/addons/godot_xterm/native/src/terminal.h @@ -37,7 +37,7 @@ private: public: std::pair get_cell_colors(const tsm_screen_attr *attr); - void draw_background(int row, int col, Color bgcol); + void draw_background(int row, int col, Color bgcol, int width); void draw_foreground(int row, int col, char *ch, const tsm_screen_attr *attr, Color fgcol);