2929import displayio
3030from micropython import const
3131
32- from adafruit_display_text import wrap_text_to_pixels , LabelBase
32+ from adafruit_display_text import wrap_text_to_pixels
3333from adafruit_display_text import bitmap_label
3434
35-
36-
37- try :
38- import bitmaptools
39- except ImportError :
40- # We have a slower fallback for bitmaptools
41- pass
42-
4335try :
4436 from typing import Optional , Tuple
4537 from fontio import FontProtocol
4941
5042# pylint: disable=too-many-instance-attributes
5143class 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+
5260 ALIGN_LEFT = const (0 )
5361 ALIGN_CENTER = const (1 )
5462 ALIGN_RIGHT = const (2 )
5563
5664 DYNAMIC_HEIGHT = const (- 1 )
5765
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 :
6769 self ._bitmap = None
6870 self ._tilegrid = None
6971 self ._prev_label_direction = None
@@ -75,12 +77,20 @@ def __init__(self, font: FontProtocol, width: int, height: int, align=ALIGN_LEFT
7577 else :
7678 self .dynamic_height = True
7779
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
7985
8086 self ._padding_left = kwargs .get ("padding_left" , 0 )
8187 self ._padding_right = kwargs .get ("padding_right" , 0 )
8288
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+ )
8494
8595 super (bitmap_label .Label , self ).__init__ (font , ** kwargs )
8696
@@ -127,22 +137,25 @@ def _place_text(
127137 if self .align == self .ALIGN_RIGHT :
128138 unused_space = self ._width - cur_line_width
129139 x_start = original_xposition + unused_space - self ._padding_right
130- xposition = x_start
140+
141+ xposition = x_start # pylint: disable=used-before-assignment
131142
132143 y_start = yposition
133- #print(f"start loc {x_start}, {y_start}")
144+ # print(f"start loc {x_start}, {y_start}")
134145
135146 left = None
136147 right = x_start
137148 top = bottom = y_start
138149 line_spacing = self ._line_spacing
139150
140- #print(f"cur_line width: {cur_line_width}")
151+ # print(f"cur_line width: {cur_line_width}")
141152 for char in text :
142153 if char == "\n " : # newline
143154 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}")
146159 if self .align == self .ALIGN_LEFT :
147160 x_start = original_xposition # starting x position (left margin)
148161 if self .align == self .ALIGN_CENTER :
@@ -225,7 +238,7 @@ def _place_text(
225238
226239 # bounding_box
227240 return left , top , right - left , bottom - top
228-
241+
229242 def _reset_text (
230243 self ,
231244 font : Optional [FontProtocol ] = None ,
@@ -314,7 +327,9 @@ def _reset_text(
314327 or self ._bitmap .width != self ._width
315328 or self ._bitmap .height != self ._height
316329 ):
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+ )
318333 self ._bitmap = new_bitmap
319334 else :
320335 self ._bitmap .fill (0 )
@@ -397,10 +412,24 @@ def height(self, height: int) -> None:
397412
398413 @bitmap_label .Label .text .setter
399414 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+ )
402418 self ._text = self ._replace_tabs (text )
403419 self ._original_text = self ._text
404420 self ._text = "\n " .join (self .lines )
405421
406422 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