/ examples / display_text_label_vs_bitmap_label_comparison.py
display_text_label_vs_bitmap_label_comparison.py
  1  # Sample for comparing label and bitmap_label positioning with Builtin or loaded BDF fonts
  2  
  3  # pylint: disable=no-member
  4  
  5  import gc
  6  import board
  7  import displayio
  8  import terminalio
  9  from adafruit_bitmap_font import bitmap_font
 10  
 11  from adafruit_display_text import bitmap_label
 12  from adafruit_display_text import label
 13  
 14  # pylint: disable=no-member
 15  
 16  
 17  ##########
 18  # Use this Boolean variables to select which font style to use
 19  ##########
 20  use_builtinfont = False  # Set True to use the terminalio.FONT BuiltinFont,
 21  fontToUse = terminalio.FONT
 22  # Set False to use a BDF loaded font, see "fontFiles" below
 23  ##########
 24  
 25  if not use_builtinfont:
 26      # load the fonts
 27      print("loading font...")
 28  
 29      fontList = []
 30  
 31      # Load some proportional fonts
 32      fontFile = "fonts/Helvetica-Bold-16.bdf"
 33      fontToUse = bitmap_font.load_font(fontFile)
 34  
 35  # Set scaling factor for display text
 36  my_scale = 1
 37  
 38  #  Setup the SPI display
 39  if "DISPLAY" not in dir(board):
 40      # Setup the LCD display with driver
 41      # You may need to change this to match the display driver for the chipset
 42      # used on your display
 43      from adafruit_ili9341 import ILI9341
 44  
 45      displayio.release_displays()
 46  
 47      # setup the SPI bus
 48      spi = board.SPI()
 49      tft_cs = board.D9  # arbitrary, pin not used
 50      tft_dc = board.D10
 51      tft_backlight = board.D12
 52      tft_reset = board.D11
 53  
 54      while not spi.try_lock():
 55          spi.configure(baudrate=32000000)
 56      spi.unlock()
 57  
 58      display_bus = displayio.FourWire(
 59          spi,
 60          command=tft_dc,
 61          chip_select=tft_cs,
 62          reset=tft_reset,
 63          baudrate=32000000,
 64          polarity=1,
 65          phase=1,
 66      )
 67  
 68      # Number of pixels in the display
 69      DISPLAY_WIDTH = 320
 70      DISPLAY_HEIGHT = 240
 71  
 72      # create the display
 73      display = ILI9341(
 74          display_bus,
 75          width=DISPLAY_WIDTH,
 76          height=DISPLAY_HEIGHT,
 77          rotation=180,  # The rotation can be adjusted to match your configuration.
 78          auto_refresh=True,
 79          native_frames_per_second=90,
 80      )
 81  
 82      # reset the display to show nothing.
 83      display.show(None)
 84  else:
 85      # built-in display
 86      display = board.DISPLAY
 87  
 88  print("Display is started")
 89  
 90  
 91  preload_glyphs = (
 92      True  # set this to True if you want to preload the font glyphs into memory
 93  )
 94  # preloading the glyphs will help speed up the rendering of text but will use more RAM
 95  
 96  if preload_glyphs and not use_builtinfont:
 97  
 98      # identify the glyphs to load into memory -> increases rendering speed
 99      glyphs = (
100          b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-_,.:?!'\n "
101      )
102  
103      print("loading glyphs...")
104      fontToUse.load_glyphs(glyphs)
105  
106      print("Glyphs are loaded.")
107  
108  print("Fonts completed loading.")
109  
110  # create group
111  
112  long_string = "The purple snake\nbrings python fun\nto everyone."
113  label2_padding = 10
114  
115  #####
116  # Create the "bitmap_label.py" versions of the text labels.
117  
118  gc.collect()
119  bitmap_label_start = gc.mem_free()
120  
121  bmap_label1 = bitmap_label.Label(
122      font=fontToUse,
123      text="bitmap_label",
124      color=0xFFFFFF,
125      background_color=0xFF0000,
126      padding_bottom=0,
127      padding_left=0,
128      padding_right=0,
129      padding_top=0,
130      background_tight=True,
131      line_spacing=1.25,
132      scale=my_scale,
133      anchor_point=(0.0, 0),
134      anchored_position=(10, 60),
135  )
136  
137  bmap_label2 = bitmap_label.Label(
138      font=fontToUse,
139      text=long_string,
140      color=0x000000,
141      max_glyphs=len(long_string),
142      background_color=0xFFFF00,
143      padding_bottom=label2_padding,
144      padding_left=0,
145      padding_right=0,
146      padding_top=label2_padding,
147      background_tight=False,
148      line_spacing=1.25,
149      scale=my_scale,
150      anchor_point=(0.0, 0),
151      anchored_position=(10, 120),
152  )
153  
154  gc.collect()
155  bitmap_label_end = gc.mem_free()
156  
157  print("bitmap_label used: {} memory".format(bitmap_label_start - bitmap_label_end))
158  
159  bmap_group = displayio.Group(max_size=4)  # Create a group for displaying
160  bmap_group.append(bmap_label1)
161  bmap_group.append(bmap_label2)
162  
163  
164  #####
165  # Create the "label.py" versions of the text labels.
166  
167  gc.collect()
168  label_start = gc.mem_free()
169  
170  label1 = label.Label(
171      font=fontToUse,
172      text="label",
173      color=0xFFFFFF,
174      background_color=0xFF0000,
175      padding_bottom=0,
176      padding_left=0,
177      padding_right=0,
178      padding_top=0,
179      background_tight=True,
180      line_spacing=1.25,
181      scale=my_scale,
182      anchor_point=(1.0, 0),
183      anchored_position=(display.width - 10, 60),
184  )
185  
186  label2 = label.Label(
187      font=fontToUse,
188      text=long_string,
189      color=0x000000,
190      max_glyphs=len(long_string),
191      background_color=0xFFFF00,
192      padding_bottom=label2_padding,
193      padding_left=0,
194      padding_right=0,
195      padding_top=label2_padding,
196      background_tight=False,
197      line_spacing=1.25,
198      scale=my_scale,
199      anchor_point=(1.0, 0),
200      anchored_position=(display.width - 10, 120),
201  )
202  
203  gc.collect()
204  label_end = gc.mem_free()
205  
206  print("label used: {} memory".format(label_start - label_end))
207  label_group = displayio.Group(max_size=4)  # Create a group for displaying
208  label_group.append(label1)
209  label_group.append(label2)
210  
211  
212  print("***")
213  
214  main_group = displayio.Group()
215  main_group.append(label_group)
216  main_group.append(bmap_group)
217  
218  display.auto_refresh = True
219  
220  display.show(main_group)
221  while True:
222      pass