Skip to content

Commit 6911723

Browse files
author
funnygeeker
committed
优化英文字符显示
1 parent 877c217 commit 6911723

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

fonts/text_full_16px_2311.v3.bmf

0 Bytes
Binary file not shown.

fonts/text_lite_16px_2311.v3.bmf

0 Bytes
Binary file not shown.

libs/easydisplay.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,29 +327,40 @@ def text(self, s: str, x: int, y: int,
327327
pass
328328

329329
dp = self.display
330-
for char in range(len(s)):
331-
if auto_wrap and ((x + font_size // 2 > dp.width and ord(s[char]) < 128 and half_char) or
332-
(x + font_size > dp.width and (not half_char or ord(s[char]) > 128))):
330+
if font_size == 16:
331+
font_offset = 12
332+
elif font_size > 16:
333+
font_offset = int(font_size * 0.69) + 1
334+
else: # 例如:8px
335+
font_offset = font_size // 2
336+
for char in s:
337+
if char in ('M', 'O', 'Q', 'V', 'W', 'X', 'm', 'w'): # 更好的适配英文字符
338+
_half_char = False
339+
else:
340+
_half_char = half_char
341+
342+
if auto_wrap and ((x + font_offset > dp.width and ord(char) < 128 and _half_char) or
343+
(x + font_size > dp.width and (not _half_char or ord(char) > 128))):
333344
y += font_size + line_spacing
334345
x = initial_x
335346

336347
# 对控制字符的处理
337-
if s[char] == '\n':
348+
if char == '\n':
338349
y += font_size + line_spacing
339350
x = initial_x
340351
continue
341-
elif s[char] == '\t':
352+
elif char == '\t':
342353
x = ((x // font_size) + 1) * font_size + initial_x % font_size
343354
continue
344-
elif ord(s[char]) < 16:
355+
elif ord(char) < 16:
345356
continue
346357

347358
# 超过范围的字符不会显示*
348359
if x > dp.width or y > dp.height:
349360
continue
350361

351362
# 获取字体的点阵数据
352-
byte_data = list(self.get_bitmap(s[char]))
363+
byte_data = list(self.get_bitmap(char))
353364

354365
# 分四种情况逐个优化
355366
# 1. 黑白屏幕/无放缩
@@ -388,8 +399,8 @@ def text(self, s: str, x: int, y: int,
388399
dp.set_window(x, y, x + font_size - 1, y + font_size + 1)
389400
dp.write_data(data)
390401
# 英文字符半格显示
391-
if ord(s[char]) < 128 and half_char:
392-
x += font_size // 2
402+
if ord(char) < 128 and _half_char:
403+
x += font_offset
393404
else:
394405
x += font_size
395406

0 commit comments

Comments
 (0)