@@ -327,29 +327,40 @@ def text(self, s: str, x: int, y: int,
327
327
pass
328
328
329
329
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 ))):
333
344
y += font_size + line_spacing
334
345
x = initial_x
335
346
336
347
# 对控制字符的处理
337
- if s [ char ] == '\n ' :
348
+ if char == '\n ' :
338
349
y += font_size + line_spacing
339
350
x = initial_x
340
351
continue
341
- elif s [ char ] == '\t ' :
352
+ elif char == '\t ' :
342
353
x = ((x // font_size ) + 1 ) * font_size + initial_x % font_size
343
354
continue
344
- elif ord (s [ char ] ) < 16 :
355
+ elif ord (char ) < 16 :
345
356
continue
346
357
347
358
# 超过范围的字符不会显示*
348
359
if x > dp .width or y > dp .height :
349
360
continue
350
361
351
362
# 获取字体的点阵数据
352
- byte_data = list (self .get_bitmap (s [ char ] ))
363
+ byte_data = list (self .get_bitmap (char ))
353
364
354
365
# 分四种情况逐个优化
355
366
# 1. 黑白屏幕/无放缩
@@ -388,8 +399,8 @@ def text(self, s: str, x: int, y: int,
388
399
dp .set_window (x , y , x + font_size - 1 , y + font_size + 1 )
389
400
dp .write_data (data )
390
401
# 英文字符半格显示
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
393
404
else :
394
405
x += font_size
395
406
0 commit comments