29
29
import displayio
30
30
from micropython import const
31
31
32
- from adafruit_display_text import wrap_text_to_pixels , LabelBase
32
+ from adafruit_display_text import wrap_text_to_pixels
33
33
from adafruit_display_text import bitmap_label
34
34
35
-
36
-
37
- try :
38
- import bitmaptools
39
- except ImportError :
40
- # We have a slower fallback for bitmaptools
41
- pass
42
-
43
35
try :
44
36
from typing import Optional , Tuple
45
37
from fontio import FontProtocol
49
41
50
42
# pylint: disable=too-many-instance-attributes
51
43
class TextBox (bitmap_label .Label ):
44
+ """
45
+ TextBox has a constrained width and optionally height.
46
+ You set the desired size when it's initialized it
47
+ will automatically wrap text to fit it within the allotted
48
+ size.
49
+
50
+ Left, Right, and Center alignment of the text within the
51
+ box are supported.
52
+
53
+ :param font: The font to use for the TextBox.
54
+ :param width: The width of the TextBox in pixels.
55
+ :param height: The height of the TextBox in pixels.
56
+ :param align: How to align the text within the box,
57
+ valid values are `ALIGN_LEFT`, `ALIGN_CENTER`, `ALIGN_RIGHT`.
58
+ """
59
+
52
60
ALIGN_LEFT = const (0 )
53
61
ALIGN_CENTER = const (1 )
54
62
ALIGN_RIGHT = const (2 )
55
63
56
64
DYNAMIC_HEIGHT = const (- 1 )
57
65
58
- def __init__ (self , font : FontProtocol , width : int , height : int , align = ALIGN_LEFT , ** kwargs ) -> None :
59
- """
60
-
61
- :param font:
62
- :param width:
63
- :param height:
64
- :param align:
65
- :param kwargs:
66
- """
66
+ def __init__ (
67
+ self , font : FontProtocol , width : int , height : int , align = ALIGN_LEFT , ** kwargs
68
+ ) -> None :
67
69
self ._bitmap = None
68
70
self ._tilegrid = None
69
71
self ._prev_label_direction = None
@@ -75,12 +77,20 @@ def __init__(self, font: FontProtocol, width: int, height: int, align=ALIGN_LEFT
75
77
else :
76
78
self .dynamic_height = True
77
79
78
- self .align = align
80
+ if align not in (TextBox .ALIGN_LEFT , TextBox .ALIGN_CENTER , TextBox .ALIGN_RIGHT ):
81
+ raise ValueError (
82
+ "Align must be one of: ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT"
83
+ )
84
+ self ._align = align
79
85
80
86
self ._padding_left = kwargs .get ("padding_left" , 0 )
81
87
self ._padding_right = kwargs .get ("padding_right" , 0 )
82
88
83
- self .lines = wrap_text_to_pixels (kwargs .get ("text" , "" ), self ._width - self ._padding_left - self ._padding_right , font )
89
+ self .lines = wrap_text_to_pixels (
90
+ kwargs .get ("text" , "" ),
91
+ self ._width - self ._padding_left - self ._padding_right ,
92
+ font ,
93
+ )
84
94
85
95
super (bitmap_label .Label , self ).__init__ (font , ** kwargs )
86
96
@@ -127,22 +137,25 @@ def _place_text(
127
137
if self .align == self .ALIGN_RIGHT :
128
138
unused_space = self ._width - cur_line_width
129
139
x_start = original_xposition + unused_space - self ._padding_right
130
- xposition = x_start
140
+
141
+ xposition = x_start # pylint: disable=used-before-assignment
131
142
132
143
y_start = yposition
133
- #print(f"start loc {x_start}, {y_start}")
144
+ # print(f"start loc {x_start}, {y_start}")
134
145
135
146
left = None
136
147
right = x_start
137
148
top = bottom = y_start
138
149
line_spacing = self ._line_spacing
139
150
140
- #print(f"cur_line width: {cur_line_width}")
151
+ # print(f"cur_line width: {cur_line_width}")
141
152
for char in text :
142
153
if char == "\n " : # newline
143
154
cur_line_index += 1
144
- cur_line_width = self ._text_bounding_box (self .lines [cur_line_index ], self .font )[0 ]
145
- #print(f"cur_line width: {cur_line_width}")
155
+ cur_line_width = self ._text_bounding_box (
156
+ self .lines [cur_line_index ], self .font
157
+ )[0 ]
158
+ # print(f"cur_line width: {cur_line_width}")
146
159
if self .align == self .ALIGN_LEFT :
147
160
x_start = original_xposition # starting x position (left margin)
148
161
if self .align == self .ALIGN_CENTER :
@@ -225,7 +238,7 @@ def _place_text(
225
238
226
239
# bounding_box
227
240
return left , top , right - left , bottom - top
228
-
241
+
229
242
def _reset_text (
230
243
self ,
231
244
font : Optional [FontProtocol ] = None ,
@@ -314,7 +327,9 @@ def _reset_text(
314
327
or self ._bitmap .width != self ._width
315
328
or self ._bitmap .height != self ._height
316
329
):
317
- new_bitmap = displayio .Bitmap (self ._width , self ._height , len (self ._palette ))
330
+ new_bitmap = displayio .Bitmap (
331
+ self ._width , self ._height , len (self ._palette )
332
+ )
318
333
self ._bitmap = new_bitmap
319
334
else :
320
335
self ._bitmap .fill (0 )
@@ -397,10 +412,24 @@ def height(self, height: int) -> None:
397
412
398
413
@bitmap_label .Label .text .setter
399
414
def text (self , text : str ) -> None :
400
- self .lines = wrap_text_to_pixels (text , self ._width - self ._padding_left - self ._padding_right ,
401
- self .font )
415
+ self .lines = wrap_text_to_pixels (
416
+ text , self ._width - self ._padding_left - self ._padding_right , self .font
417
+ )
402
418
self ._text = self ._replace_tabs (text )
403
419
self ._original_text = self ._text
404
420
self ._text = "\n " .join (self .lines )
405
421
406
422
self ._set_text (self ._text , self .scale )
423
+
424
+ @property
425
+ def align (self ):
426
+ """Alignment of the text within the TextBox"""
427
+ return self ._align
428
+
429
+ @align .setter
430
+ def align (self , align : int ) -> None :
431
+ if align not in (TextBox .ALIGN_LEFT , TextBox .ALIGN_CENTER , TextBox .ALIGN_RIGHT ):
432
+ raise ValueError (
433
+ "Align must be one of: ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT"
434
+ )
435
+ self ._align = align
0 commit comments