Skip to content

Commit cd34c85

Browse files
committed
initial commit for outlined_label
1 parent d6779ab commit cd34c85

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

adafruit_display_text/__init__.py

100755100644
File mode changed.

adafruit_display_text/bitmap_label.py

100755100644
File mode changed.

adafruit_display_text/label.py

100755100644
File mode changed.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# SPDX-FileCopyrightText: 2023 Tim C
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`adafruit_display_text.outlined_label`
7+
====================================================
8+
9+
Subclass of BitmapLabel that adds outline color and stroke size
10+
functionalities.
11+
12+
* Author(s): Tim Cocks
13+
14+
Implementation Notes
15+
--------------------
16+
17+
**Hardware:**
18+
19+
**Software and Dependencies:**
20+
21+
* Adafruit CircuitPython firmware for the supported boards:
22+
https://circuitpython.org/downloads
23+
24+
"""
25+
__version__ = "0.0.0+auto.0"
26+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
27+
28+
import bitmaptools
29+
import traceback
30+
from adafruit_display_text import bitmap_label
31+
from displayio import Palette, Bitmap
32+
33+
try:
34+
from typing import Optional, Tuple
35+
from fontio import FontProtocol
36+
except ImportError:
37+
pass
38+
39+
40+
class OutlinedLabel(bitmap_label.Label):
41+
def __init__(self, font, outline_color=0x999999, outline_size=1, **kwargs):
42+
super().__init__(font, **kwargs)
43+
44+
_background_color = self._palette[0]
45+
_foreground_color = self._palette[1]
46+
_background_is_transparent = self._palette.is_transparent(0)
47+
self._palette = Palette(3)
48+
self._palette[0] = _background_color
49+
self._palette[1] = _foreground_color
50+
self._palette[2] = outline_color
51+
if _background_is_transparent:
52+
self._palette.make_transparent(0)
53+
54+
self._outline_size = outline_size
55+
self._stamp_source = Bitmap((outline_size * 2) + 1, (outline_size * 2) + 1, 3)
56+
self._stamp_source.fill(2)
57+
58+
self._bitmap = None
59+
60+
self._reset_text(
61+
font=font,
62+
text=self._text,
63+
line_spacing=self._line_spacing,
64+
scale=self.scale,
65+
)
66+
67+
def _add_outline(self):
68+
try:
69+
# before = time.monotonic()
70+
71+
for y in range(self.bitmap.height):
72+
for x in range(self.bitmap.width):
73+
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+
)
82+
# bitmaptools.blit(bitmap, stamp_source, x - size, y - size)
83+
# for y_loc in range(-size, size+1):
84+
# for x_loc in range(-size, size+1):
85+
# if bitmap[x+x_loc, y+y_loc] != target_color_index:
86+
# 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+
94+
def _place_text(
95+
self,
96+
bitmap: Bitmap,
97+
text: str,
98+
font: FontProtocol,
99+
xposition: int,
100+
yposition: int,
101+
skip_index: int = 0, # set to None to write all pixels, other wise skip this palette index
102+
# when copying glyph bitmaps (this is important for slanted text
103+
# where rectangular glyph boxes overlap)
104+
) -> Tuple[int, int, int, int]:
105+
parent_result = super()._place_text(
106+
bitmap, text, font, xposition, yposition, skip_index=skip_index
107+
)
108+
109+
self._add_outline()
110+
111+
return parent_result
112+
113+
@property
114+
def outline_color(self):
115+
return self._palette[2]
116+
117+
@outline_color.setter
118+
def outline_color(self, new_outline_color):
119+
self._palette[2] = new_outline_color
120+
121+
@property
122+
def outline_size(self):
123+
return self._outline_size
124+
125+
@outline_size.setter
126+
def outline_size(self, new_outline_size):
127+
self._outline_size = new_outline_size
128+
self._stamp_source = Bitmap(
129+
(new_outline_size * 2) + 1, (new_outline_size * 2) + 1, 3
130+
)
131+
self._stamp_source.fill(2)
132+
self._reset_text(
133+
font=self._font,
134+
text=self._text,
135+
line_spacing=self._line_spacing,
136+
scale=self.scale,
137+
)

0 commit comments

Comments
 (0)