@@ -53,7 +53,7 @@ def __init__(self, font, *, text=None, width=None, color=0x0, height=1):
5353 width = len (text )
5454 self .width = width
5555 self .font = font
56- self ._text = text
56+ self ._text = None
5757
5858 self .p = displayio .Palette (2 )
5959 self .p .make_transparent (0 )
@@ -70,26 +70,36 @@ def __init__(self, font, *, text=None, width=None, color=0x0, height=1):
7070 self ._y = 0
7171
7272 if text :
73- self ._update_text ()
73+ self ._update_text (text )
7474
7575
76- def _update_text (self ):
76+ def _update_text (self , new_text ):
7777 x = 0
7878 i = 0
79- for c in self ._text :
79+ first_different = self ._text is not None
80+ for c in new_text :
8081 glyph = self .font .get_glyph (ord (c ))
8182 if not glyph :
8283 continue
83- face = displayio .Sprite (glyph ["bitmap" ], pixel_shader = self .p ,
84- position = (self ._x + x , self ._y + self .height - glyph ["bounds" ][1 ] - glyph ["bounds" ][3 ]))
84+ # Remove any characters that are different
85+ if first_different and c != self ._text [i ]:
86+ # TODO(tannewt): Make this smarter when we can remove and add things into the middle
87+ # of a group.
88+ for _ in range (len (self .sprites ) - i ):
89+ self .group .pop ()
90+ first_different = False
91+ if not first_different :
92+ face = displayio .Sprite (glyph ["bitmap" ], pixel_shader = self .p ,
93+ position = (self ._x + x , self ._y + self .height - glyph ["bounds" ][1 ] - glyph ["bounds" ][3 ]))
94+ self .group .append (face )
95+ self .sprites [i ] = face
8596 x += glyph ["shift" ][0 ]
86- self .group .append (face )
87- self .sprites [i ] = face
8897
8998 # TODO skip this for control sequences or non-printables.
9099 i += 1
91100
92101 # TODO: support multiple lines by adjusting y
102+ self ._text = new_text
93103
94104 @property
95105 def color (self ):
@@ -105,8 +115,7 @@ def text(self, t):
105115
106116 @text .setter
107117 def text (self , t ):
108- self ._text = t
109- self ._update_text ()
118+ self ._update_text (t )
110119
111120 @property
112121 def y (self ):
0 commit comments