Skip to content

Commit caa4100

Browse files
committed
handle padding, fix format and pylint issues
1 parent cd34c85 commit caa4100

File tree

4 files changed

+97
-22
lines changed

4 files changed

+97
-22
lines changed

adafruit_display_text/outlined_label.py

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,55 @@
2626
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
2727

2828
import bitmaptools
29-
import traceback
30-
from adafruit_display_text import bitmap_label
3129
from displayio import Palette, Bitmap
30+
from adafruit_display_text import bitmap_label
3231

3332
try:
34-
from typing import Optional, Tuple
33+
from typing import Optional, Tuple, Union
3534
from fontio import FontProtocol
3635
except ImportError:
3736
pass
3837

3938

4039
class OutlinedLabel(bitmap_label.Label):
41-
def __init__(self, font, outline_color=0x999999, outline_size=1, **kwargs):
42-
super().__init__(font, **kwargs)
40+
"""
41+
OutlinedLabel - A BitmapLabel subclass that includes arguments and properties for specifying
42+
outline_size and outline_color to get drawn as a stroke around the text.
43+
44+
:param Union[Tuple, int] outline_color: The color of the outline stroke as RGB tuple, or hex.
45+
:param int outline_size: The size in pixels of the outline stroke.
46+
47+
"""
48+
49+
# pylint: disable=too-many-arguments
50+
def __init__(
51+
self,
52+
font,
53+
outline_color: Union[int, Tuple] = 0x999999,
54+
outline_size: int = 1,
55+
padding_top: Optional[int] = None,
56+
padding_bottom: Optional[int] = None,
57+
padding_left: Optional[int] = None,
58+
padding_right: Optional[int] = None,
59+
**kwargs
60+
):
61+
if padding_top is None:
62+
padding_top = outline_size + 0
63+
if padding_bottom is None:
64+
padding_bottom = outline_size + 2
65+
if padding_left is None:
66+
padding_left = outline_size + 0
67+
if padding_right is None:
68+
padding_right = outline_size + 0
69+
70+
super().__init__(
71+
font,
72+
padding_top=padding_top,
73+
padding_bottom=padding_bottom,
74+
padding_left=padding_left,
75+
padding_right=padding_right,
76+
**kwargs
77+
)
4378

4479
_background_color = self._palette[0]
4580
_foreground_color = self._palette[1]
@@ -65,31 +100,30 @@ def __init__(self, font, outline_color=0x999999, outline_size=1, **kwargs):
65100
)
66101

67102
def _add_outline(self):
68-
try:
69-
# before = time.monotonic()
70-
103+
if hasattr(self, "_stamp_source"):
71104
for y in range(self.bitmap.height):
72105
for x in range(self.bitmap.width):
73106
if self.bitmap[x, y] == 1:
74-
# bitmap.blit(x-size,y-size, stamp_source, skip_self_index=target_color_index)
75-
bitmaptools.blit(
76-
self.bitmap,
77-
self._stamp_source,
78-
x - self._outline_size,
79-
y - self._outline_size,
80-
skip_dest_index=1,
81-
)
107+
try:
108+
bitmaptools.blit(
109+
self.bitmap,
110+
self._stamp_source,
111+
x - self._outline_size,
112+
y - self._outline_size,
113+
skip_dest_index=1,
114+
)
115+
except ValueError as value_error:
116+
raise ValueError(
117+
"Padding must be big enough to fit outline_size "
118+
"all the way around the text. "
119+
"Try using either larger padding sizes, or smaller outline_size."
120+
) from value_error
121+
82122
# bitmaptools.blit(bitmap, stamp_source, x - size, y - size)
83123
# for y_loc in range(-size, size+1):
84124
# for x_loc in range(-size, size+1):
85125
# if bitmap[x+x_loc, y+y_loc] != target_color_index:
86126
# bitmap[x + x_loc, y + y_loc] = outline_color_index
87-
except ValueError as ve:
88-
# print(traceback.print_exception(ve))
89-
pass
90-
except AttributeError as ae:
91-
# print(traceback.print_exception(ae))
92-
pass
93127

94128
def _place_text(
95129
self,
@@ -112,6 +146,7 @@ def _place_text(
112146

113147
@property
114148
def outline_color(self):
149+
"""Color of the outline to draw around the text."""
115150
return self._palette[2]
116151

117152
@outline_color.setter
@@ -120,11 +155,18 @@ def outline_color(self, new_outline_color):
120155

121156
@property
122157
def outline_size(self):
158+
"""Stroke size of the outline to draw around the text."""
123159
return self._outline_size
124160

125161
@outline_size.setter
126162
def outline_size(self, new_outline_size):
127163
self._outline_size = new_outline_size
164+
165+
self._padding_top = new_outline_size + 0
166+
self._padding_bottom = new_outline_size + 2
167+
self._padding_left = new_outline_size + 0
168+
self._padding_right = new_outline_size + 0
169+
128170
self._stamp_source = Bitmap(
129171
(new_outline_size * 2) + 1, (new_outline_size * 2) + 1, 3
130172
)

docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616

1717
.. automodule:: adafruit_display_text.scrolling_label
1818
:members:
19+
20+
.. automodule:: adafruit_display_text.outlined_label
21+
:members:

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ Simple test using scrolling_label to display text
2525
:caption: examples/display_text_scrolling_label.py
2626
:linenos:
2727

28+
OutlinedLabel Simple Test
29+
-------------------------
30+
31+
Simple test using outlined_label to display text with a stroke outline
32+
33+
.. literalinclude:: ../examples/display_text_outlined_label_simpletest.py
34+
:caption: examples/display_text_outlined_label_simpletest.py
35+
:linenos:
36+
2837
Label vs Bitmap_label Comparison
2938
--------------------------------
3039

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2023 Tim C
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import terminalio
6+
from adafruit_display_text import outlined_label
7+
8+
text = "Hello world"
9+
text_area = outlined_label.OutlinedLabel(
10+
terminalio.FONT,
11+
text=text,
12+
color=0xFF00FF,
13+
outline_color=0x00FF00,
14+
outline_size=1,
15+
scale=2,
16+
)
17+
text_area.x = 10
18+
text_area.y = 14
19+
board.DISPLAY.show(text_area)
20+
while True:
21+
pass

0 commit comments

Comments
 (0)