From 7d9a2d0648c8e45bf48041743ab983fc3b0e1b2c Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sat, 7 May 2022 10:36:55 +0200 Subject: [PATCH] replace some control characters with their symbol counterparts --- src/ui/bigtext/bigtext.py | 10 +--------- src/ui/bigtext/line.py | 26 ++++++++++++++++++++++++-- src/ui/bigtext/testline.py | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/ui/bigtext/bigtext.py b/src/ui/bigtext/bigtext.py index ea53b80..8f990cf 100644 --- a/src/ui/bigtext/bigtext.py +++ b/src/ui/bigtext/bigtext.py @@ -421,15 +421,7 @@ class InnerBigText(QWidget): left_offset = int(-1 * self._left_offset * self.char_width) y_line_offset = self.char_height; for l in self.lines: - text = l.line_tabs_replaced() - text = text.replace("\0", "␀") # qt throws error if we try to draw a null character - text = text.replace("\1", "␁") - text = text.replace("\2", "␂") - text = text.replace("\3", "␃") - text = text.replace("\4", "␄") - text = text.replace("\5", "␅") - text = text.replace("\6", "␆") - text = text.replace("\a", "␇") + text = l.line_prepared_for_display() painter.drawText(left_offset, y_line_offset, text) y_line_offset = y_line_offset + self.char_height diff --git a/src/ui/bigtext/line.py b/src/ui/bigtext/line.py index 390fefc..cb9acd2 100644 --- a/src/ui/bigtext/line.py +++ b/src/ui/bigtext/line.py @@ -36,12 +36,34 @@ class Line: prefix_chars = prefix_bytes.decode("utf8", errors="ignore") return len(prefix_chars) - def line_tabs_replaced(self): + def line_prepared_for_display(self): + line = self._line_tabs_replaced() + line = self._replace_control_chars_with_pictures(line) + return line + + def _replace_control_chars_with_pictures(self, line: str): + length = len(line) + for i in range(length): + c = line[i] + + if unicodedata.category(c) == "Cc" and c != "\r" and c != "\n" and c != "\t": + ordinal_value = ord(c) + if ordinal_value < 32 and not ordinal_value in [9, 10, 11]: + # print(ord(c), " -> ", hex(ord(c)), " -> ", unicodedata.category(c), " -> ", chr(9216 + ord(c))) + line = line.replace(c, chr(9216 + ord(c))) # see Unicode Block “Control Pictures” + else: + # print(ord(c), " -> ", hex(ord(c)), " -> ", unicodedata.category(c), " -> \u2421") + line = line.replace(c, "\u2421") # symbol for delete (␡) + # print(line) + + return line; + + def _line_tabs_replaced(self): line = self._line; i = 0 offset = 0 result = "" - length = len(line) + # length = len(line) while True: tab_index = line.find("\t", offset) if tab_index < 0: diff --git a/src/ui/bigtext/testline.py b/src/ui/bigtext/testline.py index 7297b01..6f7ab55 100644 --- a/src/ui/bigtext/testline.py +++ b/src/ui/bigtext/testline.py @@ -1,6 +1,9 @@ +import numbers import unittest import unicodedata +import base64 +import urllib from line import Line @@ -111,6 +114,20 @@ class MyTestCase(unittest.TestCase): print("%s %s cat: %s" % (c, unicodedata.name(c), unicodedata.category(c))) i = i + 1 + def test_replace_control_characters(self): + byte_offset = 0 + text = "" + for i in range(128): + if unicodedata.category(chr(i)) == "Cc": + # print(i, " -> ", ord(chr(i)), " --> ", chr(9216 + i)) + if not i in [9, 10, 11, 13]: + text = text + chr(i) + + line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text) + actual = line.line_prepared_for_display() + self.assertEqual("␀␁␂␃␄␅␆␇␈␌␎␏␐␑␒␓␔␕␖␗␘␙␚␛␜␝␞␟␡", actual) + # self.assertEqual("␀␁␂␃␄␅␆␇␈\n␎␏␐␑␒␓␔␕␖␗␘␙␚␛␜␝␞␟␡", actual) + if __name__ == '__main__': unittest.main()