/ examples / bitmap_font_displayio_simpletest.py
bitmap_font_displayio_simpletest.py
 1  """
 2  This example runs on PyPortal, or any Circuit Python device
 3  with a built-in screen that is at least 320x240 pixels.
 4  
 5  It will use adafruit_bitmap_font to load a font and fill a
 6  bitmap with pixels matching glyphs from a given String
 7  """
 8  
 9  
10  import board
11  from adafruit_bitmap_font import bitmap_font  # pylint: disable=wrong-import-position
12  import displayio
13  
14  font = bitmap_font.load_font("fonts/Arial-16.bdf")
15  
16  bitmap = displayio.Bitmap(320, 240, 2)
17  
18  palette = displayio.Palette(2)
19  
20  palette[0] = 0x000000
21  palette[1] = 0xFFFFFF
22  
23  _, height, _, dy = font.get_bounding_box()
24  for y in range(height):
25      pixels = []
26      for c in "Adafruit CircuitPython":
27          glyph = font.get_glyph(ord(c))
28          if not glyph:
29              continue
30          glyph_y = y + (glyph.height - (height + dy)) + glyph.dy
31  
32          if 0 <= glyph_y < glyph.height:
33              for i in range(glyph.width):
34                  value = glyph.bitmap[i, glyph_y]
35                  pixel = 0
36                  if value > 0:
37                      pixel = 1
38                  pixels.append(pixel)
39          else:
40              # empty section for this glyph
41              for i in range(glyph.width):
42                  pixels.append(0)
43  
44          # one space between glyph
45          pixels.append(0)
46  
47      if pixels:
48          for x, pixel in enumerate(pixels):
49              bitmap[x, y] = pixel
50  
51  # Create a TileGrid to hold the bitmap
52  tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
53  
54  # Create a Group to hold the TileGrid
55  group = displayio.Group()
56  
57  group.x = 20
58  # Add the TileGrid to the Group
59  group.append(tile_grid)
60  
61  # Add the Group to the Display
62  board.DISPLAY.show(group)
63  
64  while True:
65      pass