Skip to content

Adding more examples #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ bundles
.eggs
dist
**/*.egg-info
*.bdf
*.pcf
*.ttf
65 changes: 65 additions & 0 deletions examples/bitmap_font_displayio_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
This example runs on PyPortal, or any Circuit Python device
with a built-in screen that is at least 320x240 pixels.

It will use adafruit_bitmap_font to load a font and fill a
bitmap with pixels matching glyphs from a given String
"""


import board
from adafruit_bitmap_font import bitmap_font # pylint: disable=wrong-import-position
import displayio

font = bitmap_font.load_font("fonts/Arial-16.bdf")

bitmap = displayio.Bitmap(320, 240, 2)

palette = displayio.Palette(2)

palette[0] = 0x000000
palette[1] = 0xFFFFFF

_, height, _, dy = font.get_bounding_box()
for y in range(height):
pixels = []
for c in "Adafruit CircuitPython":
glyph = font.get_glyph(ord(c))
if not glyph:
continue
glyph_y = y + (glyph.height - (height + dy)) + glyph.dy

if 0 <= glyph_y < glyph.height:
for i in range(glyph.width):
value = glyph.bitmap[i, glyph_y]
pixel = 0
if value > 0:
pixel = 1
pixels.append(pixel)
else:
# empty section for this glyph
for i in range(glyph.width):
pixels.append(0)

# one space between glyph
pixels.append(0)

if pixels:
for x, pixel in enumerate(pixels):
bitmap[x, y] = pixel

# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)

# Create a Group to hold the TileGrid
group = displayio.Group()

group.x = 20
# Add the TileGrid to the Group
group.append(tile_grid)

# Add the Group to the Display
board.DISPLAY.show(group)

while True:
pass
28 changes: 28 additions & 0 deletions examples/bitmap_font_label_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
This example uses addfruit_display_text.label to display text using a custom font
loaded by adafruit_bitmap_font
"""

import board
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import label

display = board.DISPLAY

# Set text, font, and color
text = "HELLO WORLD"
font = bitmap_font.load_font("fonts/Arial-16.bdf")
color = 0xFF00FF

# Create the tet label
text_area = label.Label(font, text=text, color=color)

# Set the location
text_area.x = 20
text_area.y = 20

# Show it
display.show(text_area)

while True:
pass
Loading