Skip to content

Commit 946c135

Browse files
committed
label using LabelBase. line_spacing fix
1 parent 55267ee commit 946c135

File tree

3 files changed

+77
-159
lines changed

3 files changed

+77
-159
lines changed

adafruit_display_text/__init__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def scale(self, new_scale):
296296
self.local_group.scale = new_scale
297297
self.anchored_position = self._anchored_position # update the anchored_position
298298

299-
def _reset_text(self, text, scale):
299+
def _set_text(self, text, scale):
300300
# subclasses should override this
301301
pass
302302

@@ -307,4 +307,24 @@ def text(self):
307307

308308
@text.setter # Cannot set color or background color with text setter, use separate setter
309309
def text(self, new_text):
310-
self._reset_text(text=new_text, scale=self.scale)
310+
self._set_text(text=new_text, scale=self.scale)
311+
312+
@property
313+
def bounding_box(self):
314+
"""An (x, y, w, h) tuple that completely covers all glyphs. The
315+
first two numbers are offset from the x, y origin of this group"""
316+
return tuple(self._bounding_box)
317+
318+
@property
319+
def line_spacing(self):
320+
"""The amount of space between lines of text, in multiples of the font's
321+
bounding-box height. (E.g. 1.0 is the bounding-box height)"""
322+
return self._line_spacing
323+
324+
def _set_line_spacing(self, new_line_spacing):
325+
# subclass should override this.
326+
pass
327+
328+
@line_spacing.setter
329+
def line_spacing(self, new_line_spacing):
330+
self._set_line_spacing(new_line_spacing)

adafruit_display_text/bitmap_label.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -514,21 +514,8 @@ def _blit(
514514
elif y_placement > bitmap.height:
515515
break
516516

517-
@property
518-
def bounding_box(self):
519-
"""An (x, y, w, h) tuple that completely covers all glyphs. The
520-
first two numbers are offset from the x, y origin of this group"""
521-
return self._bounding_box
522517

523-
524-
@property
525-
def line_spacing(self):
526-
"""The amount of space between lines of text, in multiples of the font's
527-
bounding-box height. (E.g. 1.0 is the bounding-box height)"""
528-
return self._line_spacing
529-
530-
@line_spacing.setter
531-
def line_spacing(self, new_line_spacing):
518+
def _set_line_spacing(self, new_line_spacing):
532519
if self._save_text:
533520
self._reset_text(line_spacing=new_line_spacing, scale=self.scale)
534521
else:
@@ -539,4 +526,4 @@ def _set_font(self, new_font):
539526
if self._save_text:
540527
self._reset_text(font=new_font, scale=self.scale)
541528
else:
542-
raise RuntimeError("font is immutable when save_text is False")
529+
raise RuntimeError("font is immutable when save_text is False")

adafruit_display_text/label.py

Lines changed: 53 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
__version__ = "0.0.0-auto.0"
2828
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
2929

30+
from adafruit_display_text import LabelBase
3031

31-
class Label(displayio.Group):
32+
33+
class Label(LabelBase):
3234
"""A label displaying a string of text. The origin point set by ``x`` and ``y``
3335
properties will be the left edge of the bounding box, and in the center of a M
3436
glyph (if its one line), or the (number of lines * linespacing + M)/2. That is,
@@ -66,28 +68,12 @@ class Label(displayio.Group):
6668
# pylint: disable=too-many-instance-attributes, too-many-locals
6769
# This has a lot of getters/setters, maybe it needs cleanup.
6870

69-
def __init__(
70-
self,
71-
font,
72-
*,
73-
x=0,
74-
y=0,
75-
text="",
76-
max_glyphs=None,
77-
color=0xFFFFFF,
78-
background_color=None,
79-
line_spacing=1.25,
80-
background_tight=False,
81-
padding_top=0,
82-
padding_bottom=0,
83-
padding_left=0,
84-
padding_right=0,
85-
anchor_point=None,
86-
anchored_position=None,
87-
scale=1,
88-
base_alignment=False,
89-
**kwargs
90-
):
71+
def __init__(self, font, **kwargs):
72+
super().__init__(font, **kwargs)
73+
74+
max_glyphs = kwargs.get("max_glyphs", None)
75+
text = kwargs.get("text", None)
76+
9177
if not max_glyphs and not text:
9278
raise RuntimeError("Please provide a max size, or initial text")
9379
if not max_glyphs:
@@ -98,64 +84,69 @@ def __init__(
9884
# self Group will contain a single local_group which contains a Group (self.local_group)
9985
# which contains a TileGrid
10086
# The self scale should always be 1
101-
super().__init__(max_size=1, scale=1, **kwargs)
87+
10288
# local_group will set the scale
103-
self.local_group = displayio.Group(max_size=max_glyphs + 1, scale=scale)
89+
self.local_group = displayio.Group(
90+
max_size=max_glyphs + 1, scale=kwargs.get("scale", 1)
91+
)
10492
self.append(self.local_group)
10593

10694
self.width = max_glyphs
10795
self._font = font
10896
self._text = None
109-
self._anchor_point = anchor_point
110-
self.x = x
111-
self.y = y
97+
self._anchor_point = kwargs.get("anchor_point", None)
98+
self.x = kwargs.get("x", 0)
99+
self.y = kwargs.get("y", 0)
112100

113101
self.height = self._font.get_bounding_box()[1]
114-
self._line_spacing = line_spacing
115-
self._boundingbox = None
102+
self._line_spacing = kwargs.get("line_spacing", 1.25)
103+
self._bounding_box = None
116104

117-
self._background_tight = (
118-
background_tight # sets padding status for text background box
119-
)
105+
self._background_tight = kwargs.get(
106+
"background_tight", False
107+
) # sets padding status for text background box
120108

121109
# Create the two-color text palette
122110
self.palette = displayio.Palette(2)
123111
self.palette[0] = 0
124112
self.palette.make_transparent(0)
125-
self.color = color
113+
self.color = kwargs.get("color", 0xFFFFFF)
126114

127-
self._background_color = background_color
115+
self._background_color = kwargs.get("background_color", None)
128116
self._background_palette = displayio.Palette(1)
129117
self._added_background_tilegrid = False
130118

131-
self._padding_top = padding_top
132-
self._padding_bottom = padding_bottom
133-
self._padding_left = padding_left
134-
self._padding_right = padding_right
135-
self.base_alignment = base_alignment
119+
self._padding_top = kwargs.get("padding_top", 0)
120+
self._padding_bottom = kwargs.get("padding_bottom", 0)
121+
self._padding_left = kwargs.get("padding_left", 0)
122+
self._padding_right = kwargs.get("padding_right", 0)
123+
self.base_alignment = kwargs.get("base_alignment", False)
136124

137125
if text is not None:
126+
print("calling _update_text")
138127
self._update_text(str(text))
139-
if (anchored_position is not None) and (anchor_point is not None):
140-
self.anchored_position = anchored_position
128+
if (kwargs.get("anchored_position", None) is not None) and (
129+
kwargs.get("anchor_point", None) is not None
130+
):
131+
self.anchored_position = kwargs.get("anchored_position", None)
141132

142133
def _create_background_box(self, lines, y_offset):
143134
"""Private Class function to create a background_box
144135
:param lines: int number of lines
145136
:param y_offset: int y pixel bottom coordinate for the background_box"""
146137

147-
left = self._boundingbox[0]
138+
left = self._bounding_box[0]
148139

149140
if self._background_tight: # draw a tight bounding box
150-
box_width = self._boundingbox[2]
151-
box_height = self._boundingbox[3]
141+
box_width = self._bounding_box[2]
142+
box_height = self._bounding_box[3]
152143
x_box_offset = 0
153-
y_box_offset = self._boundingbox[1]
144+
y_box_offset = self._bounding_box[1]
154145

155146
else: # draw a "loose" bounding box to include any ascenders/descenders.
156147
ascent, descent = self._get_ascent_descent()
157148

158-
box_width = self._boundingbox[2] + self._padding_left + self._padding_right
149+
box_width = self._bounding_box[2] + self._padding_left + self._padding_right
159150
x_box_offset = -self._padding_left
160151
box_height = (
161152
(ascent + descent)
@@ -181,30 +172,6 @@ def _create_background_box(self, lines, y_offset):
181172

182173
return tile_grid
183174

184-
def _get_ascent_descent(self):
185-
""" Private function to calculate ascent and descent font values """
186-
if hasattr(self.font, "ascent"):
187-
return self.font.ascent, self.font.descent
188-
189-
# check a few glyphs for maximum ascender and descender height
190-
glyphs = "M j'" # choose glyphs with highest ascender and lowest
191-
try:
192-
self._font.load_glyphs(glyphs)
193-
except AttributeError:
194-
# Builtin font doesn't have or need load_glyphs
195-
pass
196-
# descender, will depend upon font used
197-
ascender_max = descender_max = 0
198-
for char in glyphs:
199-
this_glyph = self._font.get_glyph(ord(char))
200-
if this_glyph:
201-
ascender_max = max(ascender_max, this_glyph.height + this_glyph.dy)
202-
descender_max = max(descender_max, -this_glyph.dy)
203-
return ascender_max, descender_max
204-
205-
def _get_ascent(self):
206-
return self._get_ascent_descent()[0]
207-
208175
def _update_background_color(self, new_color):
209176
"""Private class function that allows updating the font box background color
210177
:param new_color: int color as an RGB hex number."""
@@ -227,10 +194,10 @@ def _update_background_color(self, new_color):
227194
if (
228195
(len(self._text) > 0)
229196
and (
230-
self._boundingbox[2] + self._padding_left + self._padding_right > 0
197+
self._bounding_box[2] + self._padding_left + self._padding_right > 0
231198
)
232199
and (
233-
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
200+
self._bounding_box[3] + self._padding_top + self._padding_bottom > 0
234201
)
235202
):
236203
# This can be simplified in CP v6.0, when group.append(0) bug is corrected
@@ -249,10 +216,10 @@ def _update_background_color(self, new_color):
249216
if (
250217
(len(self._text) > 0)
251218
and (
252-
self._boundingbox[2] + self._padding_left + self._padding_right > 0
219+
self._bounding_box[2] + self._padding_left + self._padding_right > 0
253220
)
254221
and (
255-
self._boundingbox[3] + self._padding_top + self._padding_bottom > 0
222+
self._bounding_box[3] + self._padding_top + self._padding_bottom > 0
256223
)
257224
):
258225
self.local_group[0] = self._create_background_box(lines, y_offset)
@@ -333,88 +300,32 @@ def _update_text(
333300
while len(self.local_group) > tilegrid_count: # i:
334301
self.local_group.pop()
335302
self._text = new_text
336-
self._boundingbox = (left, top, right - left, bottom - top)
303+
print("setting bounding box")
304+
self._bounding_box = (left, top, right - left, bottom - top)
337305

338306
if self.background_color is not None:
339307
self._update_background_color(self._background_color)
340308

341-
@property
342-
def bounding_box(self):
343-
"""An (x, y, w, h) tuple that completely covers all glyphs. The
344-
first two numbers are offset from the x, y origin of this group"""
345-
return tuple(self._boundingbox)
346-
347-
@property
348-
def line_spacing(self):
349-
"""The amount of space between lines of text, in multiples of the font's
350-
bounding-box height. (E.g. 1.0 is the bounding-box height)"""
351-
return self._line_spacing
352-
353-
@line_spacing.setter
354-
def line_spacing(self, spacing):
355-
self._line_spacing = spacing
356-
self.text = self._text # redraw the box
357-
358-
@property
359-
def color(self):
360-
"""Color of the text as an RGB hex number."""
361-
return self.palette[1]
362-
363-
@color.setter
364-
def color(self, new_color):
365-
self._color = new_color
366-
if new_color is not None:
367-
self.palette[1] = new_color
368-
self.palette.make_opaque(1)
369-
else:
370-
self.palette[1] = 0
371-
self.palette.make_transparent(1)
372-
373-
@property
374-
def background_color(self):
375-
"""Color of the background as an RGB hex number."""
376-
return self._background_color
377-
378-
@background_color.setter
379-
def background_color(self, new_color):
380-
self._update_background_color(new_color)
381-
382-
@property
383-
def text(self):
384-
"""Text to display."""
385-
return self._text
386-
387-
@text.setter
388-
def text(self, new_text):
309+
def _reset_text(self, new_text, scale):
389310
try:
390311
current_anchored_position = self.anchored_position
391312
self._update_text(str(new_text))
392313
self.anchored_position = current_anchored_position
393314
except RuntimeError as run_error:
394315
raise RuntimeError("Text length exceeds max_glyphs") from run_error
395316

396-
@property
397-
def scale(self):
398-
"""Set the scaling of the label, in integer values"""
399-
return self.local_group.scale
400-
401-
@scale.setter
402-
def scale(self, new_scale):
403-
current_anchored_position = self.anchored_position
404-
self.local_group.scale = new_scale
405-
self.anchored_position = current_anchored_position
406-
407-
@property
408-
def font(self):
409-
"""Font to use for text display."""
410-
return self._font
411-
412-
@font.setter
413-
def font(self, new_font):
317+
def _set_font(self, new_font):
414318
old_text = self._text
415319
current_anchored_position = self.anchored_position
416320
self._text = ""
417321
self._font = new_font
418322
self.height = self._font.get_bounding_box()[1]
419323
self._update_text(str(old_text))
420324
self.anchored_position = current_anchored_position
325+
326+
def _set_line_spacing(self, new_line_spacing):
327+
self._line_spacing = new_line_spacing
328+
self.text = self._text # redraw the box
329+
330+
def _set_text(self, text, scale):
331+
self._reset_text(text, scale)

0 commit comments

Comments
 (0)