2626__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
2727
2828import bitmaptools
29- import traceback
30- from adafruit_display_text import bitmap_label
3129from displayio import Palette , Bitmap
30+ from adafruit_display_text import bitmap_label
3231
3332try :
34- from typing import Optional , Tuple
33+ from typing import Optional , Tuple , Union
3534 from fontio import FontProtocol
3635except ImportError :
3736 pass
3837
3938
4039class 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 )
0 commit comments