replace some control characters with their symbol counterparts

This commit is contained in:
2022-05-07 10:36:55 +02:00
parent 4d794480c7
commit 7d9a2d0648
3 changed files with 42 additions and 11 deletions

View File

@@ -421,15 +421,7 @@ class InnerBigText(QWidget):
left_offset = int(-1 * self._left_offset * self.char_width) left_offset = int(-1 * self._left_offset * self.char_width)
y_line_offset = self.char_height; y_line_offset = self.char_height;
for l in self.lines: for l in self.lines:
text = l.line_tabs_replaced() text = l.line_prepared_for_display()
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", "")
painter.drawText(left_offset, y_line_offset, text) painter.drawText(left_offset, y_line_offset, text)
y_line_offset = y_line_offset + self.char_height y_line_offset = y_line_offset + self.char_height

View File

@@ -36,12 +36,34 @@ class Line:
prefix_chars = prefix_bytes.decode("utf8", errors="ignore") prefix_chars = prefix_bytes.decode("utf8", errors="ignore")
return len(prefix_chars) 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; line = self._line;
i = 0 i = 0
offset = 0 offset = 0
result = "" result = ""
length = len(line) # length = len(line)
while True: while True:
tab_index = line.find("\t", offset) tab_index = line.find("\t", offset)
if tab_index < 0: if tab_index < 0:

View File

@@ -1,6 +1,9 @@
import numbers
import unittest import unittest
import unicodedata import unicodedata
import base64
import urllib
from line import Line from line import Line
@@ -111,6 +114,20 @@ class MyTestCase(unittest.TestCase):
print("%s %s cat: %s" % (c, unicodedata.name(c), unicodedata.category(c))) print("%s %s cat: %s" % (c, unicodedata.name(c), unicodedata.category(c)))
i = i + 1 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__': if __name__ == '__main__':
unittest.main() unittest.main()