44
44
__version__ = "0.0.0-auto.0"
45
45
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
46
46
47
+
47
48
class Label (displayio .Group ):
48
49
"""A label displaying a string of text. The origin point set by ``x`` and ``y``
49
50
properties will be the left edge of the bounding box, and in the center of a M
@@ -56,8 +57,9 @@ class Label(displayio.Group):
56
57
:param int max_glyphs: The largest quantity of glyphs we will display
57
58
:param int color: Color of all text in RGB hex
58
59
:param double line_spacing: Line spacing of text to display"""
60
+
59
61
def __init__ (self , font , * , x = 0 , y = 0 , text = None , max_glyphs = None , color = 0xffffff ,
60
- line_spacing = 1.25 , ** kwargs ):
62
+ backgroud_color = False , line_spacing = 1.25 , ** kwargs ):
61
63
if not max_glyphs and not text :
62
64
raise RuntimeError ("Please provide a max size, or initial text" )
63
65
if not max_glyphs :
@@ -71,7 +73,10 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff
71
73
self .y = y
72
74
73
75
self .palette = displayio .Palette (2 )
74
- self .palette .make_transparent (0 )
76
+ if not backgroud_color :
77
+ self .palette .make_transparent (0 )
78
+ else :
79
+ self .palette [0 ] = backgroud_color
75
80
self .palette [1 ] = color
76
81
77
82
bounds = self .font .get_bounding_box ()
@@ -82,15 +87,14 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff
82
87
if text is not None :
83
88
self ._update_text (str (text ))
84
89
85
-
86
- def _update_text (self , new_text ): # pylint: disable=too-many-locals
90
+ def _update_text (self , new_text ): # pylint: disable=too-many-locals
87
91
x = 0
88
92
y = 0
89
93
i = 0
90
94
old_c = 0
91
95
y_offset = int ((self .font .get_glyph (ord ('M' )).height -
92
96
new_text .count ('\n ' ) * self .height * self .line_spacing ) / 2 )
93
- #print("y offset from baseline", y_offset)
97
+ # print("y offset from baseline", y_offset)
94
98
left = right = top = bottom = 0
95
99
for character in new_text :
96
100
if character == '\n ' :
@@ -100,10 +104,10 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
100
104
glyph = self .font .get_glyph (ord (character ))
101
105
if not glyph :
102
106
continue
103
- right = max (right , x + glyph .width )
104
- if y == 0 : # first line, find the Ascender height
105
- top = min (top , - glyph .height + y_offset )
106
- bottom = max (bottom , y - glyph .dy + y_offset )
107
+ right = max (right , x + glyph .width )
108
+ if y == 0 : # first line, find the Ascender height
109
+ top = min (top , - glyph .height + y_offset )
110
+ bottom = max (bottom , y - glyph .dy + y_offset )
107
111
position_y = y - glyph .height - glyph .dy + y_offset
108
112
position_x = x + glyph .dx
109
113
if not self ._text or old_c >= len (self ._text ) or character != self ._text [old_c ]:
@@ -141,7 +145,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
141
145
while len (self ) > i :
142
146
self .pop ()
143
147
self ._text = new_text
144
- self ._boundingbox = (left , top , left + right , bottom - top )
148
+ self ._boundingbox = (left , top , left + right , bottom - top )
145
149
146
150
@property
147
151
def bounding_box (self ):
@@ -192,10 +196,10 @@ def anchor_point(self, new_anchor_point):
192
196
def anchored_position (self ):
193
197
"""Position relative to the anchor_point. Tuple containing x,y
194
198
pixel coordinates."""
195
- return (self .x - self ._boundingbox [2 ]* self ._anchor_point [0 ],
196
- self .y - self ._boundingbox [3 ]* self ._anchor_point [1 ])
199
+ return (self .x - self ._boundingbox [2 ] * self ._anchor_point [0 ],
200
+ self .y - self ._boundingbox [3 ] * self ._anchor_point [1 ])
197
201
198
202
@anchored_position .setter
199
203
def anchored_position (self , new_position ):
200
- self .x = int (new_position [0 ]- (self ._boundingbox [2 ]* self ._anchor_point [0 ]))
201
- self .y = int (new_position [1 ]- (self ._boundingbox [3 ]* self ._anchor_point [1 ]))
204
+ self .x = int (new_position [0 ] - (self ._boundingbox [2 ] * self ._anchor_point [0 ]))
205
+ self .y = int (new_position [1 ] - (self ._boundingbox [3 ] * self ._anchor_point [1 ]))
0 commit comments