Skip to content

Commit 66c0ff0

Browse files
authored
Merge pull request #13 from makermelissa/master
Allows External fonts to be specified by filename
2 parents 8cd0d70 + 4a740b9 commit 66c0ff0

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

adafruit_pybadger.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import analogio
5959
from adafruit_display_shapes.rect import Rect
6060
from adafruit_display_text.label import Label
61+
from adafruit_bitmap_font import bitmap_font
6162
import terminalio
6263
import adafruit_miniqr
6364
import adafruit_lis3dh
@@ -67,6 +68,16 @@
6768

6869
Buttons = namedtuple("Buttons", "b a start select right down up left")
6970

71+
def load_font(fontname, text):
72+
"""Load a font and glyphs in the text string
73+
74+
:param str fontname: The full path to the font file.
75+
:param str text: The text containing the glyphs we want to load.
76+
77+
"""
78+
font = bitmap_font.load_font(fontname)
79+
font.load_glyphs(text.encode('utf-8'))
80+
return font
7081

7182
# pylint: disable=too-many-instance-attributes
7283
class PyBadger:
@@ -258,17 +269,21 @@ def show_business_card(self, *, image_name=None, name_string=None, name_scale=1,
258269
except AttributeError:
259270
self.display.wait_for_frame()
260271
if name_string:
272+
if isinstance(name_font, str):
273+
name_font = load_font(name_font, name_string)
261274
name_group = displayio.Group(scale=name_scale)
262-
name_label = Label(name_font, text=name_string)
275+
name_label = Label(name_font, text=name_string, line_spacing=0.75)
263276
(_, _, width, _) = name_label.bounding_box
264277
name_label.x = ((self.display.width // (2 * name_scale)) - width // 2)
265278
name_label.y = int(self.display.height * (0.73 / name_scale))
266279
name_label.color = 0xFFFFFF
267280
name_group.append(name_label)
268281
business_card_splash.append(name_group)
269282
if email_string_one:
283+
if isinstance(email_font_one, str):
284+
email_font_one = load_font(email_font_one, email_string_one)
270285
email_group_one = displayio.Group(scale=email_scale_one)
271-
email_label_one = Label(email_font_one, text=email_string_one)
286+
email_label_one = Label(email_font_one, text=email_string_one, line_spacing=0.75)
272287
(_, _, width, _) = email_label_one.bounding_box
273288
email_label_one.width = self.display.width
274289
email_label_one.x = ((self.display.width // (2 * email_scale_one)) - width // 2)
@@ -277,8 +292,10 @@ def show_business_card(self, *, image_name=None, name_string=None, name_scale=1,
277292
email_group_one.append(email_label_one)
278293
business_card_splash.append(email_group_one)
279294
if email_string_two:
295+
if isinstance(email_font_two, str):
296+
email_font_two = load_font(email_font_two, email_string_two)
280297
email_group_two = displayio.Group(scale=email_scale_two)
281-
email_label_two = Label(email_font_two, text=email_string_two)
298+
email_label_two = Label(email_font_two, text=email_string_two, line_spacing=0.75)
282299
(_, _, width, _) = email_label_two.bounding_box
283300
email_label_two.width = self.display.width
284301
email_label_two.x = ((self.display.width // (2 * email_scale_two)) - width // 2)
@@ -329,22 +346,28 @@ def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
329346
(int(self.display.height * 0.5)), fill=foreground_color)
330347
splash.append(rect)
331348

349+
if isinstance(hello_font, str):
350+
hello_font = load_font(hello_font, hello_string)
332351
hello_group = displayio.Group(scale=hello_scale)
333-
hello_label = Label(font=hello_font, text=hello_string)
352+
hello_label = Label(font=hello_font, text=hello_string, line_spacing=0.75)
334353
(_, _, width, _) = hello_label.bounding_box
335354
hello_label.x = ((self.display.width // (2 * hello_scale)) - width // 2)
336355
hello_label.y = int(self.display.height * (0.117 / hello_scale))
337356
hello_label.color = background_text_color
338357
hello_group.append(hello_label)
339358

359+
if isinstance(my_name_is_font, str):
360+
my_name_is_font = load_font(my_name_is_font, my_name_is_string)
340361
my_name_is_group = displayio.Group(scale=my_name_is_scale)
341-
my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string)
362+
my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string, line_spacing=0.75)
342363
(_, _, width, _) = my_name_is_label.bounding_box
343364
my_name_is_label.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2)
344365
my_name_is_label.y = int(self.display.height * (0.28 / my_name_is_scale))
345366
my_name_is_label.color = background_text_color
346367
my_name_is_group.append(my_name_is_label)
347368

369+
if isinstance(name_font, str):
370+
name_font = load_font(name_font, name_string)
348371
name_group = displayio.Group(scale=name_scale)
349372
name_label = Label(font=name_font, text=name_string, line_spacing=0.75)
350373
(_, _, width, _) = name_label.bounding_box

docs/conf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
# autodoc module docs will fail to generate with a warning.
2323
autodoc_mock_imports = ["audioio", "displayio", "gamepadshift", "neopixel", "analogio",
2424
"adafruit_display_shapes", "adafruit_display_text", "terminalio",
25-
"adafruit_miniqr", "adafruit_lis3dh", "adafruit_miniqr"]
25+
"adafruit_miniqr", "adafruit_lis3dh", "adafruit_miniqr",
26+
"adafruit_bitmap_font"]
2627

2728

2829
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

0 commit comments

Comments
 (0)