From: Nicolas Pitre <nico@fluxnic.net>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jirislaby@kernel.org>
Cc: Nicolas Pitre <npitre@baylibre.com>,
linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 03/14] vt: properly support zero-width Unicode code points
Date: Thu, 17 Apr 2025 14:45:05 -0400 [thread overview]
Message-ID: <20250417184849.475581-4-nico@fluxnic.net> (raw)
In-Reply-To: <20250417184849.475581-1-nico@fluxnic.net>
From: Nicolas Pitre <npitre@baylibre.com>
Zero-width Unicode code points are causing misalignment in vertically
aligned content, disrupting the visual layout. Let's handle zero-width
code points more intelligently.
Double-width code points are stored in the screen grid followed by a white
space code point to create the expected screen layout. When a double-width
code point is followed by a zero-width code point in the console incoming
bytestream (e.g., an emoji with a presentation selector) then we may
replace the white space padding by that zero-width code point instead of
dropping it. This maximize screen content information while preserving
proper layout.
If a zero-width code point is preceded by a single-width code point then
the above trick is not possible and such zero-width code point must
be dropped.
VS16 (Variation Selector 16, U+FE0F) is special as it typically doubles
the width of the preceding single-width code point. We handle that case
by giving VS16 a width of 1 instead of 0 when that happens.
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Reviewed-by: Jiri Slaby <jirislaby@kernel.org>
---
drivers/tty/vt/vt.c | 70 ++++++++++++++++++++++++++++++++++++--
include/linux/consolemap.h | 10 ++++++
2 files changed, 78 insertions(+), 2 deletions(-)
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index bcb508bc15..a989feffad 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -443,6 +443,15 @@ static void vc_uniscr_scroll(struct vc_data *vc, unsigned int top,
}
}
+static u32 vc_uniscr_getc(struct vc_data *vc, int relative_pos)
+{
+ int pos = vc->state.x + vc->vc_need_wrap + relative_pos;
+
+ if (vc->vc_uni_lines && in_range(pos, 0, vc->vc_cols))
+ return vc->vc_uni_lines[vc->state.y][pos];
+ return 0;
+}
+
static void vc_uniscr_copy_area(u32 **dst_lines,
unsigned int dst_cols,
unsigned int dst_rows,
@@ -2905,6 +2914,60 @@ static bool vc_is_control(struct vc_data *vc, int tc, int c)
return false;
}
+static void vc_con_rewind(struct vc_data *vc)
+{
+ if (vc->state.x && !vc->vc_need_wrap) {
+ vc->vc_pos -= 2;
+ vc->state.x--;
+ }
+ vc->vc_need_wrap = 0;
+}
+
+#define UCS_VS16 0xfe0f /* Variation Selector 16 */
+
+static int vc_process_ucs(struct vc_data *vc, int c, int *tc)
+{
+ u32 prev_c, curr_c = c;
+
+ if (ucs_is_double_width(curr_c))
+ return 2;
+
+ if (!ucs_is_zero_width(curr_c))
+ return 1;
+
+ /* From here curr_c is known to be zero-width. */
+
+ if (ucs_is_double_width(vc_uniscr_getc(vc, -2))) {
+ /*
+ * Let's merge this zero-width code point with the preceding
+ * double-width code point by replacing the existing
+ * whitespace padding. To do so we rewind one column and
+ * pretend this has a width of 1.
+ * We give the legacy display the same initial space padding.
+ */
+ vc_con_rewind(vc);
+ *tc = ' ';
+ return 1;
+ }
+
+ /* From here the preceding character, if any, must be single-width. */
+ prev_c = vc_uniscr_getc(vc, -1);
+
+ if (curr_c == UCS_VS16 && prev_c != 0) {
+ /*
+ * VS16 (U+FE0F) is special. It typically turns the preceding
+ * single-width character into a double-width one. Let it
+ * have a width of 1 effectively making the combination with
+ * the preceding character double-width.
+ */
+ *tc = ' ';
+ return 1;
+ }
+
+ /* Otherwise zero-width code points are ignored. */
+ return 0;
+}
+
static int vc_con_write_normal(struct vc_data *vc, int tc, int c,
struct vc_draw_region *draw)
{
@@ -2915,8 +2978,9 @@ static int vc_con_write_normal(struct vc_data *vc, int tc, int c,
bool inverse = false;
if (vc->vc_utf && !vc->vc_disp_ctrl) {
- if (ucs_is_double_width(c))
- width = 2;
+ width = vc_process_ucs(vc, c, &tc);
+ if (!width)
+ goto out;
}
/* Now try to find out how to display it */
@@ -2995,6 +3059,8 @@ static int vc_con_write_normal(struct vc_data *vc, int tc, int c,
tc = ' ';
next_c = ' ';
}
+
+out:
notify_write(vc, c);
if (inverse)
diff --git a/include/linux/consolemap.h b/include/linux/consolemap.h
index caf079bcb8..7d778752dc 100644
--- a/include/linux/consolemap.h
+++ b/include/linux/consolemap.h
@@ -29,6 +29,11 @@ u32 conv_8bit_to_uni(unsigned char c);
int conv_uni_to_8bit(u32 uni);
void console_map_init(void);
bool ucs_is_double_width(uint32_t cp);
+static inline bool ucs_is_zero_width(uint32_t cp)
+{
+ /* coming soon */
+ return false;
+}
#else
static inline u16 inverse_translate(const struct vc_data *conp, u16 glyph,
bool use_unicode)
@@ -63,6 +68,11 @@ static inline bool ucs_is_double_width(uint32_t cp)
{
return false;
}
+
+static inline bool ucs_is_zero_width(uint32_t cp)
+{
+ return false;
+}
#endif /* CONFIG_CONSOLE_TRANSLATIONS */
#endif /* __LINUX_CONSOLEMAP_H__ */
--
2.49.0
next prev parent reply other threads:[~2025-04-17 18:49 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-17 18:45 [PATCH v3 00/14] vt: implement proper Unicode handling Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 01/14] vt: minor cleanup to vc_translate_unicode() Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 02/14] vt: move unicode processing to a separate file Nicolas Pitre
2025-04-17 18:45 ` Nicolas Pitre [this message]
2025-04-17 18:45 ` [PATCH v3 04/14] vt: introduce gen_ucs_width_table.py to create ucs_width_table.h Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 05/14] vt: create ucs_width_table.h with gen_ucs_width_table.py Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 06/14] vt: use new tables in ucs.c Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 07/14] vt: introduce gen_ucs_recompose_table.py to create ucs_recompose_table.h Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 08/14] vt: create ucs_recompose_table.h with gen_ucs_recompose_table.py Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 09/14] vt: support Unicode recomposition Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 10/14] vt: pad double-width code points with a zero-width space Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 11/14] vt: remove zero-width-space handling from conv_uni_to_pc() Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 12/14] vt: update gen_ucs_width_table.py to make tables more space efficient Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 13/14] vt: refresh ucs_width_table.h and adjust code in ucs.c accordingly Nicolas Pitre
2025-04-17 18:45 ` [PATCH v3 14/14] vt: move UCS tables to the "shipped" form Nicolas Pitre
2025-04-25 14:29 ` [PATCH v3 00/14] vt: implement proper Unicode handling Greg Kroah-Hartman
2025-04-25 16:13 ` Nicolas Pitre
2025-04-26 9:24 ` Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250417184849.475581-4-nico@fluxnic.net \
--to=nico@fluxnic.net \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=npitre@baylibre.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®