@@ -25,8 +25,6 @@ def _print_bytes(self, bytes):
2525 bytes (bytes): Bytes to write to stdout
2626 """
2727 self .s .send (bytes )
28- # with os.fdopen(os.open(self.device, os.O_WRONLY), "wb", closefd=False) as dev:
29- # dev.write(bytes)
3028
3129 def print_text (self , text ):
3230 """
@@ -35,85 +33,95 @@ def print_text(self, text):
3533 Args:
3634 text (str): Text to print
3735 """
38- text = text .split (" " )
39- chunk = ""
40- text_lines = []
41- for word in text :
42- delimiter = "" if len (chunk ) in [0 , MAX_CHARS_PER_LINE ] else " "
43- if len (word ) + len (chunk ) + len (delimiter ) > MAX_CHARS_PER_LINE :
44- # flush the chunk and make a new chunk
45-
46- if len (chunk ) != 0 and len (chunk ) <= MAX_CHARS_PER_LINE :
47- text_lines .append (chunk )
48- chunk = word
49- else :
50- # single word longer than MAX_CHARS_PER_LINE
51- word_hunks = [
52- chunk [i : i + MAX_CHARS_PER_LINE - 1 ]
53- for i in range (0 , len (chunk ), MAX_CHARS_PER_LINE - 1 )
54- ]
55- for word_hunk in word_hunks [:- 2 ]:
56- text_lines .append (word_hunk + "-" )
57- if len (word_hunks [- 1 ]) == 1 :
58- text_lines .append (word_hunks [- 2 ] + word_hunks [- 1 ])
59- else :
60- text_lines .append (word_hunks [- 2 ] + "-" )
61- chunk = word_hunks [- 1 ]
62- delimiter = "" if len (chunk ) in [0 , MAX_CHARS_PER_LINE ] else " "
63- if len (word ) + len (chunk ) + len (delimiter ) > MAX_CHARS_PER_LINE :
36+ newline_separated_text_lines = []
37+ newline_separated_text = text .split ("\n " )
38+ for newline_chunk in newline_separated_text :
39+ words = newline_chunk .split (" " )
40+ chunk = ""
41+ text_lines = []
42+ for word in words :
43+ delimiter = "" if len (chunk ) in [0 , MAX_CHARS_PER_LINE ] else " "
44+ if len (word ) + len (chunk ) + len (delimiter ) > MAX_CHARS_PER_LINE :
45+ # flush the chunk and make a new chunk
46+
47+ if len (chunk ) != 0 and len (chunk ) <= MAX_CHARS_PER_LINE :
6448 text_lines .append (chunk )
6549 chunk = word
6650 else :
67- chunk = chunk + delimiter + word
68- else :
69- chunk = chunk + delimiter + word
70- # flush last chunk
71- if len (chunk ) <= MAX_CHARS_PER_LINE :
72- text_lines .append (chunk )
73- else :
74- word_hunks = [
75- chunk [i : i + MAX_CHARS_PER_LINE - 1 ]
76- for i in range (0 , len (chunk ), MAX_CHARS_PER_LINE - 1 )
77- ]
78- for word_hunk in word_hunks [:- 2 ]:
79- text_lines .append (word_hunk + "-" )
80- if len (word_hunks [- 1 ]) == 1 :
81- text_lines .append (word_hunks [- 2 ] + word_hunks [- 1 ])
51+ # single word longer than MAX_CHARS_PER_LINE
52+ word_hunks = [
53+ chunk [i : i + MAX_CHARS_PER_LINE - 1 ]
54+ for i in range (0 , len (chunk ), MAX_CHARS_PER_LINE - 1 )
55+ ]
56+ for word_hunk in word_hunks [:- 2 ]:
57+ text_lines .append (word_hunk + "-" )
58+ if len (word_hunks [- 1 ]) == 1 :
59+ text_lines .append (word_hunks [- 2 ] + word_hunks [- 1 ])
60+ else :
61+ text_lines .append (word_hunks [- 2 ] + "-" )
62+ chunk = word_hunks [- 1 ]
63+ delimiter = "" if len (chunk ) in [0 , MAX_CHARS_PER_LINE ] else " "
64+ if len (word ) + len (chunk ) + len (delimiter ) > MAX_CHARS_PER_LINE :
65+ text_lines .append (chunk )
66+ chunk = word
67+ else :
68+ chunk = chunk + delimiter + word
69+ else :
70+ chunk = chunk + delimiter + word
71+ # flush last chunk
72+ if len (chunk ) <= MAX_CHARS_PER_LINE :
73+ text_lines .append (chunk )
8274 else :
83- text_lines .append (word_hunks [- 2 ] + "-" )
84- text_lines .append (word_hunks [- 1 ])
75+ word_hunks = [
76+ chunk [i : i + MAX_CHARS_PER_LINE - 1 ]
77+ for i in range (0 , len (chunk ), MAX_CHARS_PER_LINE - 1 )
78+ ]
79+ for word_hunk in word_hunks [:- 2 ]:
80+ text_lines .append (word_hunk + "-" )
81+ if len (word_hunks [- 1 ]) == 1 :
82+ text_lines .append (word_hunks [- 2 ] + word_hunks [- 1 ])
83+ else :
84+ text_lines .append (word_hunks [- 2 ] + "-" )
85+ text_lines .append (word_hunks [- 1 ])
86+
87+ newline_separated_text_lines .append (text_lines )
8588
8689 line_data_list = [b"" for i in range (LINE_HEIGHT_BITS )]
8790
8891 self ._print_bytes (HEADER )
89- for text_line in text_lines :
90- bytes_per_line = len (text_line ) * 5
91- if bytes_per_line > MAX_CHARS_PER_LINE * 5 :
92- raise SystemExit (
93- "Line too long to print; failed to split correctly?\n [{}]" .format (
94- text_line
92+ for index , text_lines in enumerate (newline_separated_text_lines ):
93+ for text_line in text_lines :
94+ bytes_per_line = len (text_line ) * 5
95+ if bytes_per_line > MAX_CHARS_PER_LINE * 5 :
96+ raise SystemExit (
97+ "Line too long to print; failed to split correctly?\n [{}]" .format (
98+ text_line
99+ )
95100 )
101+
102+ BLOCK_MARKER = (
103+ GSV0
104+ + bytes ([bytes_per_line ])
105+ + b"\x00 "
106+ + bytes ([LINE_HEIGHT_BITS ])
107+ + b"\x00 "
96108 )
97109
98- BLOCK_MARKER = (
99- GSV0
100- + bytes ([ bytes_per_line ])
101- + b" \x00 "
102- + bytes ([ LINE_HEIGHT_BITS ])
103- + b" \x00 "
104- )
105- self . _print_bytes ( BLOCK_MARKER )
110+ line_data = line_data_list . copy ()
111+ for char in text_line :
112+ try :
113+ char_bytes_list = charset [ char ]
114+ except KeyError :
115+ char_bytes_list = charset [ "CHAR_NOT_FOUND" ]
116+ for index , char_bytes in enumerate ( char_bytes_list ):
117+ line_data [ index ] += char_bytes
106118
107- line_data = line_data_list .copy ()
108- for char in text_line :
109- char_bytes_list = charset [char ]
110- for index , char_bytes in enumerate (char_bytes_list ):
111- line_data [index ] += char_bytes
119+ self ._print_bytes (BLOCK_MARKER )
120+ for bit_line in line_data :
121+ self ._print_bytes (bit_line )
112122
113- for bit_line in line_data :
114- self ._print_bytes (bit_line )
123+ self ._print_bytes (PRINT_FEED )
115124
116- self ._print_bytes (PRINT_FEED )
117125 self ._print_bytes (PRINT_FEED )
118126 self ._print_bytes (FOOTER )
119127
0 commit comments