From 2bce43f792afc09dfdbdf25f112e184e959e8b79 Mon Sep 17 00:00:00 2001 From: Christian Ludwig Date: Thu, 8 Dec 2022 17:45:19 +0100 Subject: [PATCH 1/2] Fix: TTkString alignment (at _hasSpecialWidth) --- TermTk/TTkCore/string.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/TermTk/TTkCore/string.py b/TermTk/TTkCore/string.py index c03a4878..7247aa3d 100644 --- a/TermTk/TTkCore/string.py +++ b/TermTk/TTkCore/string.py @@ -312,17 +312,19 @@ class TTkString(): if unicodedata.category(ch) in ('Me','Mn'): rt += ch continue - if sz == width: - ret._text = rt - ret._colors = self._colors[:len(rt)] - break - elif sz > width: - ret._text = rt[:-1]+TTkCfg.theme.unicodeWideOverflowCh[1] + delta_sz = 2 if unicodedata.east_asian_width(ch) == 'W' else 1 # (*) + if sz + delta_sz <= width: + rt += ch + sz += delta_sz + if sz == width: + ret._text = rt + ret._colors = self._colors[:len(rt)] + break + else: # sz +delta_sz == width + 1, because of (*) + ret._text = rt+TTkCfg.theme.unicodeWideOverflowCh[1] ret._colors = self._colors[:len(ret._text)] ret._colors[-1] = TTkCfg.theme.unicodeWideOverflowColor break - rt += ch - sz += 2 if unicodedata.east_asian_width(ch) == 'W' else 1 else: # Legacy, trim the string ret._text = self._text[:width] From a127422daf7dae93a2cc5bd14c4fa0c5b3aefb23 Mon Sep 17 00:00:00 2001 From: Christian Ludwig Date: Thu, 8 Dec 2022 21:09:47 +0100 Subject: [PATCH 2/2] Fix: TTkString alignment; refactor --- TermTk/TTkCore/string.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/TermTk/TTkCore/string.py b/TermTk/TTkCore/string.py index 7247aa3d..2ae61327 100644 --- a/TermTk/TTkCore/string.py +++ b/TermTk/TTkCore/string.py @@ -309,19 +309,18 @@ class TTkString(): rt = "" sz = 0 for ch in self._text: + rt += ch if unicodedata.category(ch) in ('Me','Mn'): - rt += ch continue - delta_sz = 2 if unicodedata.east_asian_width(ch) == 'W' else 1 # (*) - if sz + delta_sz <= width: - rt += ch - sz += delta_sz - if sz == width: - ret._text = rt - ret._colors = self._colors[:len(rt)] - break - else: # sz +delta_sz == width + 1, because of (*) - ret._text = rt+TTkCfg.theme.unicodeWideOverflowCh[1] + + sz += 2 if unicodedata.east_asian_width(ch) == 'W' else 1 + + if sz == width: + ret._text = rt + ret._colors = self._colors[:len(rt)] + break + elif sz > width: + ret._text = rt[:-1]+TTkCfg.theme.unicodeWideOverflowCh[1] ret._colors = self._colors[:len(ret._text)] ret._colors[-1] = TTkCfg.theme.unicodeWideOverflowColor break