26
26
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
27
27
28
28
import bitmaptools
29
- import traceback
30
- from adafruit_display_text import bitmap_label
31
29
from displayio import Palette , Bitmap
30
+ from adafruit_display_text import bitmap_label
32
31
33
32
try :
34
- from typing import Optional , Tuple
33
+ from typing import Optional , Tuple , Union
35
34
from fontio import FontProtocol
36
35
except ImportError :
37
36
pass
38
37
39
38
40
39
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
+ )
43
78
44
79
_background_color = self ._palette [0 ]
45
80
_foreground_color = self ._palette [1 ]
@@ -65,31 +100,30 @@ def __init__(self, font, outline_color=0x999999, outline_size=1, **kwargs):
65
100
)
66
101
67
102
def _add_outline (self ):
68
- try :
69
- # before = time.monotonic()
70
-
103
+ if hasattr (self , "_stamp_source" ):
71
104
for y in range (self .bitmap .height ):
72
105
for x in range (self .bitmap .width ):
73
106
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
+
82
122
# bitmaptools.blit(bitmap, stamp_source, x - size, y - size)
83
123
# for y_loc in range(-size, size+1):
84
124
# for x_loc in range(-size, size+1):
85
125
# if bitmap[x+x_loc, y+y_loc] != target_color_index:
86
126
# 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
93
127
94
128
def _place_text (
95
129
self ,
@@ -112,6 +146,7 @@ def _place_text(
112
146
113
147
@property
114
148
def outline_color (self ):
149
+ """Color of the outline to draw around the text."""
115
150
return self ._palette [2 ]
116
151
117
152
@outline_color .setter
@@ -120,11 +155,18 @@ def outline_color(self, new_outline_color):
120
155
121
156
@property
122
157
def outline_size (self ):
158
+ """Stroke size of the outline to draw around the text."""
123
159
return self ._outline_size
124
160
125
161
@outline_size .setter
126
162
def outline_size (self , new_outline_size ):
127
163
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
+
128
170
self ._stamp_source = Bitmap (
129
171
(new_outline_size * 2 ) + 1 , (new_outline_size * 2 ) + 1 , 3
130
172
)
0 commit comments