diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index aa868cd..9f65598 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -95,10 +95,7 @@ def __init__( self.width = max_glyphs self._font = font self._text = None - if anchor_point is None: - self._anchor_point = (0, 0) - else: - self._anchor_point = anchor_point + self._anchor_point = anchor_point self.x = x self.y = y @@ -126,7 +123,7 @@ def __init__( if text is not None: self._update_text(str(text)) - if anchored_position is not None: + if (anchored_position is not None) and (anchor_point is not None): self.anchored_position = anchored_position def _create_background_box(self, lines, y_offset): @@ -374,14 +371,19 @@ def anchor_point(self): @anchor_point.setter def anchor_point(self, new_anchor_point): - current_anchored_position = self.anchored_position - self._anchor_point = new_anchor_point - self.anchored_position = current_anchored_position + if self._anchor_point is not None: + current_anchored_position = self.anchored_position + self._anchor_point = new_anchor_point + self.anchored_position = current_anchored_position + else: + self._anchor_point = new_anchor_point @property def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" + if self._anchor_point is None: + return None return ( int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale)), int( @@ -393,6 +395,8 @@ def anchored_position(self): @anchored_position.setter def anchored_position(self, new_position): + if (self._anchor_point is None) or (new_position is None): + return # Note: anchor_point must be set before setting anchored_position new_x = int( new_position[0] - self._anchor_point[0] * (self._boundingbox[2] * self._scale) diff --git a/examples/display_text_bitmap_label_simpletest.py b/examples/display_text_bitmap_label_simpletest.py new file mode 100644 index 0000000..acb2ca0 --- /dev/null +++ b/examples/display_text_bitmap_label_simpletest.py @@ -0,0 +1,12 @@ +import board +import terminalio +from adafruit_display_text import bitmap_label + + +text = "Hello world" +text_area = bitmap_label.Label(terminalio.FONT, text=text) +text_area.x = 10 +text_area.y = 10 +board.DISPLAY.show(text_area) +while True: + pass diff --git a/examples/display_text_label_vs_bitmap_label_alternating.py b/examples/display_text_label_vs_bitmap_label_alternating.py deleted file mode 100755 index 41b0d22..0000000 --- a/examples/display_text_label_vs_bitmap_label_alternating.py +++ /dev/null @@ -1,370 +0,0 @@ -# Sample for comparing label and bitmap_label positioning with Builtin or loaded BDF fonts - -# pylint: disable=no-member - -import gc -import time -import board -import displayio -import terminalio -from adafruit_bitmap_font import bitmap_font - -from adafruit_display_text import bitmap_label - -from adafruit_display_text import label - -# pylint: disable=no-member - -# Setup the SPI display -########## -# Use this Boolean variables to select which font style to use -########## -use_builtinfont = True # Set True to use the terminalio.FONT BuiltinFont, -# Set False to use a BDF loaded font, see "fontFiles" below -########## - -# Set scaling factor for display text -my_scale = 1 - -# Setup the SPI display - -if "DISPLAY" not in dir(board): - # Setup the LCD display with driver - # You may need to change this to match the display driver for the chipset - # used on your display - from adafruit_ili9341 import ILI9341 - - displayio.release_displays() - - # setup the SPI bus - spi = board.SPI() - tft_cs = board.D9 # arbitrary, pin not used - tft_dc = board.D10 - tft_backlight = board.D12 - tft_reset = board.D11 - - while not spi.try_lock(): - spi.configure(baudrate=32000000) - spi.unlock() - - display_bus = displayio.FourWire( - spi, - command=tft_dc, - chip_select=tft_cs, - reset=tft_reset, - baudrate=32000000, - polarity=1, - phase=1, - ) - - # Number of pixels in the display - DISPLAY_WIDTH = 320 - DISPLAY_HEIGHT = 240 - - # create the display - display = ILI9341( - display_bus, - width=DISPLAY_WIDTH, - height=DISPLAY_HEIGHT, - rotation=180, # The rotation can be adjusted to match your configuration. - auto_refresh=True, - native_frames_per_second=90, - ) - - # reset the display to show nothing. - display.show(None) -else: - # built-in display - display = board.DISPLAY - -print("Display is started") - - -# load all the fonts -print("loading fonts...") - -fontList = [] - -# Load some proportional fonts -fontFiles = [ - "fonts/Helvetica-Bold-16.bdf", - # 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2 - # 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText -] - - -for i, fontFile in enumerate(fontFiles): - - if use_builtinfont: - thisFont = ( - terminalio.FONT - ) # comment this out to switch back to BDF loaded fonts - else: - thisFont = bitmap_font.load_font(fontFile) - - fontList.append(thisFont) - - -preload_glyphs = ( - True # set this to True if you want to preload the font glyphs into memory -) -# preloading the glyphs will help speed up the rendering of text but will use more RAM - -if preload_glyphs: - - # identify the glyphs to load into memory -> increases rendering speed - glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! " - - print("loading glyphs...") - for font in fontList: - if font is not terminalio.FONT: - font.load_glyphs(glyphs) - - print("Glyphs are loaded.") - - -print("Fonts completed loading.") - -# create group - -my_string1 = "This is a label.py and\nbitmap_label.py comparison." -my_string23 = "none" -my_string_bitmap_label = "bitmap_label" -my_string_label = "label bitmap_label" - - -##### -# Create the "bitmap_label.py" versions of the text labels. - -gc.collect() -bitmap_label_start = gc.mem_free() - -bmap_label1 = bitmap_label.Label( - font=fontList[0], - text=my_string1, - color=0xFFFFFF, - max_glyphs=len(my_string1), - background_color=0xFF0000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=60, - line_spacing=1.25, - scale=my_scale, - anchor_point=(0.5, 0), - anchored_position=(160, 50), -) -label2_padding = 10 -bmap_label2 = bitmap_label.Label( - font=fontList[0], - text=my_string23, - color=0x000000, - max_glyphs=len(my_string23), - background_color=0xFFFF00, - padding_bottom=label2_padding, - padding_left=0, - padding_right=0, - padding_top=label2_padding, - background_tight=False, - x=10, - y=100, - line_spacing=1.25, - scale=my_scale, - anchor_point=(0, 0), - anchored_position=(200, 150), -) - -bmap_label3 = bitmap_label.Label( - font=fontList[0], - text=my_string23, - color=0x000000, - max_glyphs=len(my_string23), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=150, - line_spacing=1.25, - scale=my_scale, -) - -bmap_label4 = bitmap_label.Label( - font=fontList[0], - text=my_string_label, - color=0x000000, - max_glyphs=len(my_string_label), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=False, - x=10, - y=200, - line_spacing=1.25, - scale=my_scale, -) - -my_string5 = "bitmap_label -->" -bmap_label5 = bitmap_label.Label( - font=fontList[0], - text=my_string5, - color=0xFFFFFF, - max_glyphs=len(my_string5), - background_color=0x000000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=200, - line_spacing=1.25, - anchor_point=(1, 0.5), - anchored_position=(200, 200), - scale=my_scale, -) - - -gc.collect() -bitmap_label_end = gc.mem_free() - -bmap_group = displayio.Group(max_size=5) # Create a group for displaying -bmap_group.append(bmap_label1) -bmap_group.append(bmap_label2) -bmap_group.append(bmap_label3) -bmap_group.append(bmap_label4) -bmap_group.append(bmap_label5) - - -##### -# Create the "label.py" versions of the text labels. - -gc.collect() -label_start = gc.mem_free() - -label1 = label.Label( - font=fontList[0], - text=my_string1, - color=0xFFFFFF, - max_glyphs=len(my_string1), - background_color=0xFF0000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=60, - line_spacing=1.25, - scale=my_scale, - anchor_point=(0.5, 0), - anchored_position=(160, 50), -) - -label2 = label.Label( - font=fontList[0], - text=my_string23, - color=0x000000, - max_glyphs=len(my_string23), - background_color=0xFFFF00, - padding_bottom=label2_padding, - padding_left=0, - padding_right=0, - padding_top=label2_padding, - background_tight=False, - x=10, - y=100, - line_spacing=1.25, - scale=my_scale, - anchor_point=(0, 0), - anchored_position=(200, 150), -) - -label3 = label.Label( - font=fontList[0], - text=my_string23, - color=0x000000, - max_glyphs=len(my_string23), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=150, - line_spacing=1.25, - scale=my_scale, -) - -label4 = label.Label( - font=fontList[0], - text=my_string_label, - color=0x000000, - max_glyphs=len(my_string_label), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=200, - line_spacing=1.25, - scale=my_scale, -) - - -my_string5 = "<-- label" -label5 = label.Label( - font=fontList[0], - text=my_string5, - color=0xFFFFFF, - max_glyphs=len(my_string5), - background_color=0x000000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=False, - x=10, - y=200, - line_spacing=1.25, - anchor_point=(0, 0.5), - anchored_position=(50, 200), - scale=my_scale, -) - -gc.collect() -label_end = gc.mem_free() - -label_group = displayio.Group(max_size=5) # Create a group for displaying -label_group.append(label1) -label_group.append(label2) -label_group.append(label3) -label_group.append(label4) -label_group.append(label5) - - -print("***") - -display.auto_refresh = True - -while True: - print("bitmap_label") - time.sleep(0.1) - display.show(bmap_group) - - time.sleep(2) - - print("label") - time.sleep(0.1) - display.show(label_group) - time.sleep(2) diff --git a/examples/display_text_label_vs_bitmap_label_comparison.py b/examples/display_text_label_vs_bitmap_label_comparison.py new file mode 100644 index 0000000..a9bde6c --- /dev/null +++ b/examples/display_text_label_vs_bitmap_label_comparison.py @@ -0,0 +1,221 @@ +# Sample for comparing label and bitmap_label positioning with Builtin or loaded BDF fonts + +# pylint: disable=no-member + +import gc +import time +import board +import displayio +import terminalio +from adafruit_bitmap_font import bitmap_font + +from adafruit_display_text import bitmap_label +from adafruit_display_text import label + +# pylint: disable=no-member + + +########## +# Use this Boolean variables to select which font style to use +########## +use_builtinfont = False # Set True to use the terminalio.FONT BuiltinFont, +fontToUse = terminalio.FONT +# Set False to use a BDF loaded font, see "fontFiles" below +########## + +if not use_builtinfont: + # load the fonts + print("loading font...") + + fontList = [] + + # Load some proportional fonts + fontFile = "fonts/Helvetica-Bold-16.bdf" + fontToUse = bitmap_font.load_font(fontFile) + +# Set scaling factor for display text +my_scale = 1 + +# Setup the SPI display +if "DISPLAY" not in dir(board): + # Setup the LCD display with driver + # You may need to change this to match the display driver for the chipset + # used on your display + from adafruit_ili9341 import ILI9341 + + displayio.release_displays() + + # setup the SPI bus + spi = board.SPI() + tft_cs = board.D9 # arbitrary, pin not used + tft_dc = board.D10 + tft_backlight = board.D12 + tft_reset = board.D11 + + while not spi.try_lock(): + spi.configure(baudrate=32000000) + spi.unlock() + + display_bus = displayio.FourWire( + spi, + command=tft_dc, + chip_select=tft_cs, + reset=tft_reset, + baudrate=32000000, + polarity=1, + phase=1, + ) + + # Number of pixels in the display + DISPLAY_WIDTH = 320 + DISPLAY_HEIGHT = 240 + + # create the display + display = ILI9341( + display_bus, + width=DISPLAY_WIDTH, + height=DISPLAY_HEIGHT, + rotation=180, # The rotation can be adjusted to match your configuration. + auto_refresh=True, + native_frames_per_second=90, + ) + + # reset the display to show nothing. + display.show(None) +else: + # built-in display + display = board.DISPLAY + +print("Display is started") + + + +preload_glyphs = ( + True # set this to True if you want to preload the font glyphs into memory +) +# preloading the glyphs will help speed up the rendering of text but will use more RAM + +if preload_glyphs and not use_builtinfont: + + # identify the glyphs to load into memory -> increases rendering speed + glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! " + + print("loading glyphs...") + fontToUse.load_glyphs(glyphs) + + print("Glyphs are loaded.") + +print("Fonts completed loading.") + +# create group + +long_string = "The purple snake\nbrings python fun\nto everyone." + +##### +# Create the "bitmap_label.py" versions of the text labels. + +gc.collect() +bitmap_label_start = gc.mem_free() + +bmap_label1 = bitmap_label.Label( + font=fontToUse, + text="bitmap_label", + color=0xFFFFFF, + background_color=0xFF0000, + padding_bottom=0, + padding_left=0, + padding_right=0, + padding_top=0, + background_tight=True, + line_spacing=1.25, + scale=my_scale, + anchor_point=(0.0, 0), + anchored_position=(10, 60), +) +label2_padding = 10 +bmap_label2 = bitmap_label.Label( + font=fontToUse, + text=long_string, + color=0x000000, + max_glyphs=len(long_string), + background_color=0xFFFF00, + padding_bottom=label2_padding, + padding_left=0, + padding_right=0, + padding_top=label2_padding, + background_tight=False, + line_spacing=1.25, + scale=my_scale, + anchor_point=(0.0, 0), + anchored_position=(10, 120), +) + +gc.collect() +bitmap_label_end = gc.mem_free() + +print("bitmap_label used: {} memory".format(bitmap_label_start - bitmap_label_end)) + +bmap_group = displayio.Group(max_size=4) # Create a group for displaying +bmap_group.append(bmap_label1) +bmap_group.append(bmap_label2) + + +##### +# Create the "label.py" versions of the text labels. + +gc.collect() +label_start = gc.mem_free() + +label1 = label.Label( + font=fontToUse, + text="label", + color=0xFFFFFF, + background_color=0xFF0000, + padding_bottom=0, + padding_left=0, + padding_right=0, + padding_top=0, + background_tight=True, + line_spacing=1.25, + scale=my_scale, + anchor_point=(1.0, 0), + anchored_position=(display.width-10, 60), +) + +label2 = label.Label( + font=fontToUse, + text=long_string, + color=0x000000, + max_glyphs=len(long_string), + background_color=0xFFFF00, + padding_bottom=label2_padding, + padding_left=0, + padding_right=0, + padding_top=label2_padding, + background_tight=False, + line_spacing=1.25, + scale=my_scale, + anchor_point=(1.0, 0), + anchored_position=(display.width-10, 120), +) + +gc.collect() +label_end = gc.mem_free() + +print("label used: {} memory".format(label_start - label_end)) +label_group = displayio.Group(max_size=4) # Create a group for displaying +label_group.append(label1) +label_group.append(label2) + + +print("***") + +main_group = displayio.Group() +main_group.append(label_group) +main_group.append(bmap_group) + +display.auto_refresh = True + +display.show(main_group) +while True: + pass \ No newline at end of file diff --git a/examples/display_text_label_vs_bitmap_label_simpletest.py b/examples/display_text_label_vs_bitmap_label_simpletest.py deleted file mode 100755 index 5565a26..0000000 --- a/examples/display_text_label_vs_bitmap_label_simpletest.py +++ /dev/null @@ -1,180 +0,0 @@ -# Sample for comparing label and bitmap_label memory usage with Builtin or loaded BDF fonts - -import gc -import time -import board -import displayio -import terminalio - - -# pylint: disable=no-member - -########## -# Use these Boolean variables to select the text display library and which font style to use -########## -use_bitmaplabel = True # Set True if to use 'bitmap_label.py' -# Set False to use 'label.py' library -########## -use_builtinfont = True # Set True to use the terminalio.FONT BuiltinFont, -# Set False to use a BDF loaded font, see "fontFiles" below -########## - - -my_scale = 1 - -if use_bitmaplabel: # use bitmap_label.py library (Bitmap) - from adafruit_display_text import bitmap_label as label - - version = "bitmap_label.py" - -else: # use label.py library (TileGrid) - from adafruit_display_text import label - - version = "label.py" - -# Setup the SPI display - -if "DISPLAY" not in dir(board): - # Setup the LCD display with driver - # You may need to change this to match the display driver for the chipset - # used on your display - from adafruit_ili9341 import ILI9341 - - displayio.release_displays() - - # setup the SPI bus - spi = board.SPI() - tft_cs = board.D9 # arbitrary, pin not used - tft_dc = board.D10 - tft_backlight = board.D12 - tft_reset = board.D11 - - while not spi.try_lock(): - spi.configure(baudrate=32000000) - spi.unlock() - - display_bus = displayio.FourWire( - spi, - command=tft_dc, - chip_select=tft_cs, - reset=tft_reset, - baudrate=32000000, - polarity=1, - phase=1, - ) - - # Number of pixels in the display - DISPLAY_WIDTH = 320 - DISPLAY_HEIGHT = 240 - - # create the display - display = ILI9341( - display_bus, - width=DISPLAY_WIDTH, - height=DISPLAY_HEIGHT, - rotation=180, # The rotation can be adjusted to match your configuration. - auto_refresh=True, - native_frames_per_second=90, - ) - - # reset the display to show nothing. - display.show(None) -else: - # built-in display - display = board.DISPLAY - -print("Display is started") - -# load all the fonts -print("loading fonts...") - - -# Setup file locations for BDF font files -font_files = [ - "fonts/Helvetica-Bold-16.bdf", - # 'fonts/BitstreamVeraSans-Roman-24.bdf', - # 'fonts/BitstreamVeraSans-Roman-16.bdf', - # 'fonts/Fayette-HandwrittenScript-64.bdf', -] - -font_list = [] - -for i, font_file in enumerate(font_files): - - if use_builtinfont: - this_font = ( - terminalio.FONT - ) # comment this out to switch back to BDF loaded fonts - else: - from adafruit_bitmap_font import bitmap_font - - this_font = bitmap_font.load_font(font_file) - - font_list.append(this_font) - - -preload_the_glyphs = ( - True # set this to True if you want to preload the font glyphs into memory -) -# preloading the glyphs will help speed up the rendering of text but will use more RAM - -if preload_the_glyphs: - - # identify the glyphs to load into memory -> increases rendering speed - glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! " - - print("loading glyphs...") - for font in font_list: - if font is not terminalio.FONT: - font.load_glyphs(glyphs) - - print("Glyphs are loaded.") - -print("Fonts completed loading.") - -# create group - -gc.collect() -print("After creating Group, Memory free: {}".format(gc.mem_free())) - - -my_string = "Welcome to using displayio on CircuitPython!" - -gc.collect() -label_start_memory = gc.mem_free() -start_time = time.monotonic() - -bmap_label = label.Label( - font=font_list[0], - text=my_string, - color=0xFFFFFF, - max_glyphs=len(my_string), - background_color=0xFF0000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=False, - x=10, - y=30, - line_spacing=1.25, - scale=my_scale, -) -end_time = time.monotonic() - -gc.collect() -label_end_memory = gc.mem_free() - -bmap_group = displayio.Group(max_size=1) # Create a group for displaying -bmap_group.append(bmap_label) - - -print("***") -print("{} memory used: {}".format(version, label_start_memory - label_end_memory)) -print("{} time to process: {} seconds".format(version, end_time - start_time)) -display.auto_refresh = True - -display.show(bmap_group) - -while True: - pass