45
45
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
46
46
47
47
48
- class TextArea :
49
- def __init__ (self , font , * , text = None , width = None , color = 0x0 , height = 1 ):
48
+ class TextArea ( displayio . Group ) :
49
+ def __init__ (self , font , * , text = None , width = None , color = 0xffffff , height = 1 ):
50
50
if not width and not text :
51
51
raise RuntimeError ("Please provide a width" )
52
+ super ().__init__ (max_size = width * height )
52
53
if not width :
53
54
width = len (text )
54
55
self .width = width
@@ -59,16 +60,9 @@ def __init__(self, font, *, text=None, width=None, color=0x0, height=1):
59
60
self .p .make_transparent (0 )
60
61
self .p [1 ] = color
61
62
62
- self .group = displayio .Group (max_size = width * height )
63
-
64
63
bounds = self .font .get_bounding_box ()
65
64
self .height = bounds [1 ]
66
65
67
- self .sprites = [None ] * (width * height )
68
-
69
- self ._x = 0
70
- self ._y = 0
71
-
72
66
if text :
73
67
self ._update_text (text )
74
68
@@ -86,30 +80,21 @@ def _update_text(self, new_text):
86
80
glyph = self .font .get_glyph (ord (c ))
87
81
if not glyph :
88
82
continue
89
- # Remove any characters that are different
90
- if first_different and c != self ._text [i ]:
91
- # TODO(tannewt): Make this smarter when we can remove and add things into the middle
92
- # of a group.
93
- for _ in range (len (self .sprites ) - i ):
94
- try :
95
- self .group .pop ()
96
- except IndexError :
97
- break
98
- first_different = False
99
- if not first_different :
100
- position = (self ._x + x , self ._y + y + self .height - glyph ["bounds" ][1 ] - glyph ["bounds" ][3 ])
101
- try :
102
- face = displayio .TileGrid (glyph ["bitmap" ], pixel_shader = self .p , position = position )
103
- except :
104
- face = displayio .Sprite (glyph ["bitmap" ], pixel_shader = self .p , position = position )
105
- self .group .append (face )
106
- self .sprites [i ] = face
107
- x += glyph ["shift" ][0 ]
83
+ if not self ._text or i >= len (self ._text ) or c != self ._text [i ]:
84
+ face = displayio .TileGrid (glyph .bitmap , pixel_shader = self .p , default_tile = glyph .tile_index ,
85
+ tile_width = glyph .width , tile_height = glyph .height ,
86
+ position = (x , y + self .height - glyph .height - glyph .dy ))
87
+ if i < len (self ):
88
+ self [i ] = face
89
+ else :
90
+ self .append (face )
91
+ x += glyph .shift_x
108
92
109
93
# TODO skip this for control sequences or non-printables.
110
94
i += 1
111
-
112
- # TODO: support multiple lines by adjusting y
95
+ # Remove the rest
96
+ while len (self ) > i :
97
+ self .pop ()
113
98
self ._text = new_text
114
99
115
100
@property
@@ -127,29 +112,3 @@ def text(self, t):
127
112
@text .setter
128
113
def text (self , t ):
129
114
self ._update_text (t )
130
-
131
- @property
132
- def y (self ):
133
- return self ._y
134
-
135
- @y .setter
136
- def y (self , new_y ):
137
- for sprite in self .sprites :
138
- if not sprite :
139
- continue
140
- pos = sprite .position
141
- sprite .position = (pos [0 ], (pos [1 ] - self ._y ) + new_y )
142
- self ._y = new_y
143
-
144
- @property
145
- def x (self ):
146
- return self ._x
147
-
148
- @x .setter
149
- def x (self , new_x ):
150
- for sprite in self .sprites :
151
- if not sprite :
152
- continue
153
- pos = sprite .position
154
- sprite .position = ((pos [0 ] - self ._x ) + new_x , pos [1 ])
155
- self ._x = new_x
0 commit comments