From b08bb0bdc35ad2400086000ff2c63d5926480c84 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sat, 28 Aug 2021 02:02:03 +0000 Subject: [PATCH] LibGfx: Return 0 width for non-printable ASCII characters Non-printable characters should always have a width of 0. This is not true for some characters like tab, but those can be exempted as the need arises. Doing this here saves us from a bunch of checks in any place that needs to figure out glyph widths for text which can contain non-printable characters. --- Userland/Libraries/LibGfx/BitmapFont.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibGfx/BitmapFont.h b/Userland/Libraries/LibGfx/BitmapFont.h index 2bdbfbe8d0..36e8d3192d 100644 --- a/Userland/Libraries/LibGfx/BitmapFont.h +++ b/Userland/Libraries/LibGfx/BitmapFont.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -45,7 +46,13 @@ public: Glyph glyph(u32 code_point) const override; bool contains_glyph(u32 code_point) const override { return code_point < (u32)glyph_count() && m_glyph_widths[code_point] > 0; } - u8 glyph_width(size_t ch) const override { return m_fixed_width ? m_glyph_width : m_glyph_widths[ch]; } + u8 glyph_width(size_t ch) const override + { + if (is_ascii(ch) && !is_ascii_printable(ch)) + return 0; + + return m_fixed_width ? m_glyph_width : m_glyph_widths[ch]; + } ALWAYS_INLINE int glyph_or_emoji_width(u32 code_point) const override { if (m_fixed_width)