Skip to content

Commit b8137b0

Browse files
committed
Handle missing characters and newlines
1 parent 1b8494b commit b8137b0

File tree

4 files changed

+120
-68
lines changed

4 files changed

+120
-68
lines changed

phomemo_printer/ESCPOS_printer.py

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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

phomemo_printer/pixel_sans/charset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,5 @@
8686
"\\": CHAR_BACKSLASH,
8787
" ": CHAR_space,
8888
"+": CHAR_LEMON,
89+
"CHAR_NOT_FOUND": CHAR_NOT_FOUND,
8990
}

phomemo_printer/pixel_sans/symbols.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,46 @@
814814
b"\x00\x00\x00\x00\x00",
815815
b"\x00\x00\x00\x00\x00",
816816
]
817+
818+
CHAR_NOT_FOUND = [
819+
b"\xFF\xFF\xFF\xFF\xFF",
820+
b"\xFF\xFF\xFF\xFF\xFF",
821+
b"\xFF\xFF\xFF\xFF\xFF",
822+
b"\xFF\xFF\xFF\xFF\xFF",
823+
b"\xFF\xFF\xFF\xFF\xFF",
824+
b"\xFF\xFE\x00\x7F\xFF",
825+
b"\xFF\xFE\x00\x7F\xFF",
826+
b"\xFF\xFE\x00\x7F\xFF",
827+
b"\xFF\xFE\x00\x7F\xFF",
828+
b"\xFF\xFE\x00\x7F\xFF",
829+
b"\xFF\xFF\xFF\xFF\xFF",
830+
b"\xFF\xFF\xFF\xFF\xFF",
831+
b"\xFF\xFF\xFF\xFF\xFF",
832+
b"\xFF\xFF\xFF\xFF\xFF",
833+
b"\xFF\xFF\xFF\xFF\xFF",
834+
b"\xFF\xC0\x00\x7F\xFF",
835+
b"\xFF\xC0\x00\x7F\xFF",
836+
b"\xFF\xC0\x00\x7F\xFF",
837+
b"\xFF\xC0\x00\x7F\xFF",
838+
b"\xFF\xC0\x00\x7F\xFF",
839+
b"\xF8\x01\xFF\xFF\xFF",
840+
b"\xF8\x01\xFF\xFF\xFF",
841+
b"\xF8\x01\xFF\xFF\xFF",
842+
b"\xF8\x01\xFF\xFF\xFF",
843+
b"\xF8\x01\xFF\xFF\xFF",
844+
b"\xF8\x01\xFF\x80\x1F",
845+
b"\xF8\x01\xFF\x80\x1F",
846+
b"\xF8\x01\xFF\x80\x1F",
847+
b"\xF8\x01\xFF\x80\x1F",
848+
b"\xF8\x01\xFF\x80\x1F",
849+
b"\xFF\xC0\x00\x03\xFF",
850+
b"\xFF\xC0\x00\x03\xFF",
851+
b"\xFF\xC0\x00\x03\xFF",
852+
b"\xFF\xC0\x00\x03\xFF",
853+
b"\xFF\xC0\x00\x03\xFF",
854+
b"\xFF\xFF\xFF\xFF\xFF",
855+
b"\xFF\xFF\xFF\xFF\xFF",
856+
b"\xFF\xFF\xFF\xFF\xFF",
857+
b"\xFF\xFF\xFF\xFF\xFF",
858+
b"\xFF\xFF\xFF\xFF\xFF",
859+
]

phomemo_printer/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.2"
1+
__version__ = "1.0.3"

0 commit comments

Comments
 (0)