diff --git a/examples/display_text_antialias_lvfont_example.py b/examples/display_text_antialias_lvfont_example.py new file mode 100644 index 0000000..3d2e755 --- /dev/null +++ b/examples/display_text_antialias_lvfont_example.py @@ -0,0 +1,121 @@ +# SPDX-FileCopyrightText: 2026 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT + +""" +Expanded example demonstrating LVGL-format fonts with multiple bpp values. + +This script shows font samples and labels for 8bpp, 4bpp, 2bpp, and 1bpp +fonts. Update the font filenames below to match what is on your CIRCUITPY +drive. +""" + +import supervisor +import terminalio +from adafruit_bitmap_font import bitmap_font +from displayio import Bitmap, Group, Palette, TileGrid + +from adafruit_display_text.bitmap_label import Label + +display = supervisor.runtime.display + +FONT_SAMPLES = [ + { + "label": "8bpp", + "font_file": "fonts/goudy_bookletter_1911_20px_8bit.bin", + "bpp": 8, + }, + { + "label": "4bpp", + "font_file": "fonts/goudy_bookletter_1911_20px_4bit.bin", + "bpp": 4, + }, + { + "label": "2bpp", + "font_file": "fonts/goudy_bookletter_1911_20px_2bit.bin", + "bpp": 2, + }, + { + "label": "1bpp", + "font_file": "fonts/goudy_bookletter_1911_20px_1bit.bin", + "bpp": 1, + }, +] + +SAMPLE_TEXT = "The quick brown fox 123" + + +def make_gradient_palette(start_color: int, end_color: int, count: int) -> Palette: + if count <= 0: + raise ValueError("count must be > 0") + palette = Palette(count) + + sr, sg, sb = (start_color >> 16) & 0xFF, (start_color >> 8) & 0xFF, start_color & 0xFF + er, eg, eb = (end_color >> 16) & 0xFF, (end_color >> 8) & 0xFF, end_color & 0xFF + + for i in range(count): + if count == 1: + r, g, b = sr, sg, sb + else: + t = i / (count - 1) + r = int(sr + (er - sr) * t) + g = int(sg + (eg - sg) * t) + b = int(sb + (eb - sb) * t) + palette[i] = (r << 16) | (g << 8) | b + + return palette + + +def load_font(font_file: str, label: str): + font = bitmap_font.load_font(font_file) + print(f"Loaded {label} font from {font_file}") + return font + + +main_group = Group() + +# white background +bg_group = Group(scale=8) +bg_bmp = Bitmap(display.width // 8, display.height // 8, 1) +bg_palette = Palette(1) +bg_palette[0] = 0xFFFFFF +bg_tg = TileGrid(bg_bmp, pixel_shader=bg_palette) +bg_group.append(bg_tg) +main_group.append(bg_group) + +# font samples and labels +x_label = 10 +x_sample = 70 +y_cursor = 10 +line_spacing = 8 + +for entry in FONT_SAMPLES: + font = load_font(entry["font_file"], entry["label"]) + + label = Label(terminalio.FONT, text=entry["label"], color=0x000000, scale=2) + label.anchor_point = (0, 0) + label.anchored_position = (x_label, y_cursor) + main_group.append(label) + + if entry["bpp"] > 1: + palette = make_gradient_palette(0xFFFFFF, 0x000000, 2 ** entry["bpp"]) + sample = Label(font, text=SAMPLE_TEXT, color_palette=palette) + else: + sample = Label(font, text=SAMPLE_TEXT, background_color=0xFFFFFF, color=0x000000) + + sample.anchor_point = (0, 0) + sample.anchored_position = (x_sample, y_cursor) + main_group.append(sample) + + try: + _, bbox_height, _, _ = font.get_bounding_box() + line_height = bbox_height + except AttributeError: + line_height = 24 + + y_cursor += max(line_height, 20) + line_spacing + + +display.root_group = main_group + +while True: + pass diff --git a/fonts/LeagueSpartan-Bold-16.bdf b/examples/fonts/LeagueSpartan-Bold-16.bdf similarity index 100% rename from fonts/LeagueSpartan-Bold-16.bdf rename to examples/fonts/LeagueSpartan-Bold-16.bdf diff --git a/fonts/LeagueSpartan-Bold-16.bdf.license b/examples/fonts/LeagueSpartan-Bold-16.bdf.license similarity index 100% rename from fonts/LeagueSpartan-Bold-16.bdf.license rename to examples/fonts/LeagueSpartan-Bold-16.bdf.license diff --git a/fonts/LibreBodoniv2002-Bold-27.bdf b/examples/fonts/LibreBodoniv2002-Bold-27.bdf similarity index 100% rename from fonts/LibreBodoniv2002-Bold-27.bdf rename to examples/fonts/LibreBodoniv2002-Bold-27.bdf diff --git a/fonts/LibreBodoniv2002-Bold-27.bdf.license b/examples/fonts/LibreBodoniv2002-Bold-27.bdf.license similarity index 100% rename from fonts/LibreBodoniv2002-Bold-27.bdf.license rename to examples/fonts/LibreBodoniv2002-Bold-27.bdf.license diff --git a/examples/fonts/goudy_bookletter_1911_20px_1bit.bin b/examples/fonts/goudy_bookletter_1911_20px_1bit.bin new file mode 100644 index 0000000..dc65f16 Binary files /dev/null and b/examples/fonts/goudy_bookletter_1911_20px_1bit.bin differ diff --git a/examples/fonts/goudy_bookletter_1911_20px_1bit.bin.license b/examples/fonts/goudy_bookletter_1911_20px_1bit.bin.license new file mode 100644 index 0000000..20fee83 --- /dev/null +++ b/examples/fonts/goudy_bookletter_1911_20px_1bit.bin.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2009, Barry Schwartz, with Reserved Font Name OFL "Goudy Bookletter 1911" + +# SPDX-License-Identifier: OFL-1.1 diff --git a/examples/fonts/goudy_bookletter_1911_20px_2bit.bin b/examples/fonts/goudy_bookletter_1911_20px_2bit.bin new file mode 100644 index 0000000..235e197 Binary files /dev/null and b/examples/fonts/goudy_bookletter_1911_20px_2bit.bin differ diff --git a/examples/fonts/goudy_bookletter_1911_20px_2bit.bin.license b/examples/fonts/goudy_bookletter_1911_20px_2bit.bin.license new file mode 100644 index 0000000..20fee83 --- /dev/null +++ b/examples/fonts/goudy_bookletter_1911_20px_2bit.bin.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2009, Barry Schwartz, with Reserved Font Name OFL "Goudy Bookletter 1911" + +# SPDX-License-Identifier: OFL-1.1 diff --git a/examples/fonts/goudy_bookletter_1911_20px_4bit.bin b/examples/fonts/goudy_bookletter_1911_20px_4bit.bin new file mode 100644 index 0000000..e113034 Binary files /dev/null and b/examples/fonts/goudy_bookletter_1911_20px_4bit.bin differ diff --git a/examples/fonts/goudy_bookletter_1911_20px_4bit.bin.license b/examples/fonts/goudy_bookletter_1911_20px_4bit.bin.license new file mode 100644 index 0000000..20fee83 --- /dev/null +++ b/examples/fonts/goudy_bookletter_1911_20px_4bit.bin.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2009, Barry Schwartz, with Reserved Font Name OFL "Goudy Bookletter 1911" + +# SPDX-License-Identifier: OFL-1.1 diff --git a/examples/fonts/goudy_bookletter_1911_20px_8bit.bin b/examples/fonts/goudy_bookletter_1911_20px_8bit.bin new file mode 100644 index 0000000..473837c Binary files /dev/null and b/examples/fonts/goudy_bookletter_1911_20px_8bit.bin differ diff --git a/examples/fonts/goudy_bookletter_1911_20px_8bit.bin.license b/examples/fonts/goudy_bookletter_1911_20px_8bit.bin.license new file mode 100644 index 0000000..20fee83 --- /dev/null +++ b/examples/fonts/goudy_bookletter_1911_20px_8bit.bin.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2009, Barry Schwartz, with Reserved Font Name OFL "Goudy Bookletter 1911" + +# SPDX-License-Identifier: OFL-1.1