From 6ff4188ca7c389f3c64f552e6e31fc51261543fa Mon Sep 17 00:00:00 2001 From: kmatch98 <33587466+kmatch98@users.noreply.github.com> Date: Fri, 5 Jun 2020 17:00:48 -0500 Subject: [PATCH 1/7] Revert "Added background color capability." --- examples/code.py | 180 ------------------ examples/textmap_simpletest.py | 8 +- examples/textmap_simpletest2.py | 91 --------- .../textmap_simpletest2_builtin_screen.py | 66 ------- examples/textmap_simpletest_builtin_screen.py | 11 +- textmap.py | 85 +++------ 6 files changed, 39 insertions(+), 402 deletions(-) delete mode 100644 examples/code.py delete mode 100755 examples/textmap_simpletest2.py delete mode 100755 examples/textmap_simpletest2_builtin_screen.py diff --git a/examples/code.py b/examples/code.py deleted file mode 100644 index 85252ca..0000000 --- a/examples/code.py +++ /dev/null @@ -1,180 +0,0 @@ -# Sample code using the textMap library and the "textBox" wrapper class -# Creates four textBox instances -# Inserts each textBox into a tileGrid group -# Writes text into the box one character at a time -# Moves the position of the textBox around the display -# Clears each textBox after the full string is written (even if the text is outside of the box) - -import textmap -from textmap import textBox - -import board -import displayio -import time -import terminalio -import fontio -import sys -import busio -#from adafruit_st7789 import ST7789 -from adafruit_ili9341 import ILI9341 - -# Setup the SPI display - -print('Starting the display...') # goes to serial only -displayio.release_displays() - - -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) - pass -spi.unlock() - -display_bus = displayio.FourWire( - spi, - command=tft_dc, - chip_select=tft_cs, - reset=tft_reset, - baudrate=32000000, - polarity=1, - phase=1, -) - -print('spi.frequency: {}'.format(spi.frequency)) - -DISPLAY_WIDTH=320 -DISPLAY_HEIGHT=240 - -#display = ST7789(display_bus, width=240, height=240, rotation=0, rowstart=80, colstart=0) -display = ILI9341(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, rotation=180, auto_refresh=True) - -display.show(None) - -print ('Display is started') - - -# load all the fonts -print('loading fonts...') - -import terminalio - - -fontList = [] -fontHeight = [] - -##### the BuiltinFont terminalio.FONT has a different return strategy for get_glyphs and -# is currently not handled by these functions. -#fontList.append(terminalio.FONT) -#fontHeight = [10] # somehow the terminalio.FONT needs to be adjusted to 10 - -# Load some proportional fonts -fontFiles = [ - 'fonts/Helvetica-Bold-16.bdf', - 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2 - 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText - ] - -from adafruit_bitmap_font import bitmap_font - -for i, fontFile in enumerate(fontFiles): - thisFont = bitmap_font.load_font(fontFile) - fontList.append(thisFont) - fontHeight.append( thisFont.get_glyph(ord("M")).height ) - -preloadTheGlyphs= 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 preloadTheGlyphs: - - # identify the glyphs to load into memory -> increases rendering speed - glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! ' - - print('loading glyphs...') - for font in fontList: - font.load_glyphs(glyphs) - print('Glyphs are loaded.') - -print('Fonts completed loading.') - -# create group -import gc -gc.collect() -print( 'Memory free: {}'.format(gc.mem_free()) ) - -textBoxes=[] # list of textBox instances - -textBoxes.append( textBox('', fontList[0], DISPLAY_WIDTH, DISPLAY_HEIGHT, backgroundColor=0x000000, textColor=0x443344) ) -print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[0], 150, 60, backgroundColor=0x000000, textColor=0xFFFFFF) ) -print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[1], 160, 100, backgroundColor=0xFF00FF, textColor=0xFFFFFF) ) -print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[2], 180, 80, backgroundColor=0x00FFFF, textColor=0x444444) ) -print( 'Memory free: {}'.format(gc.mem_free()) ) -gc.collect() -myGroup = displayio.Group( max_size=len(textBoxes) ) # Create a group for displaying - -tileGridList=[] # list of tileGrids - - -#startPositions -x=[0, 10, 160, 50] -y=[0, 20, 80, 150] - -xVelocity=[0, 1, -1, 2] -yVelocity=[0, -1, 2, 1] - -gc.collect() -stringList=[] -stringList.append('Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box.') -stringList.append('Helvetica Bold 16 font - with Black background color') -stringList.append('Vera Sans 24 font - this is a longer line that is wrapping around') -stringList.append('Vera Sans 16 font - how much text will this hold but it will not print text that goes outside the box but it will cut it off at the bottom if it is too large.') - -for i, box in enumerate(textBoxes): - tileGridList.append (displayio.TileGrid(box.bitmap, pixel_shader=box.palette, x=x[i], y=y[i]) ) - myGroup.append(tileGridList[i]) -display.show(myGroup) - -charCount=0 -while True: - - # Add characters one at a time. - - for i, box in enumerate(textBoxes): - charToPrint=charCount % len(stringList[i]) - if charToPrint == 0: - box.clearBitmap() - box.addText(stringList[i][charToPrint]) # add a character - gc.collect() - - charCount += 1 - - # Move each box - - for i, thisTileGrid in enumerate(tileGridList): - targetX=thisTileGrid.x + xVelocity[i] - targetY=thisTileGrid.y + yVelocity[i] - if ( (targetX + textBoxes[i].bitmap.width) >= DISPLAY_WIDTH ) or (targetX < 0): - xVelocity[i] = -1* xVelocity[i] - if ( (targetY + textBoxes[i].bitmap.height) >= DISPLAY_HEIGHT ) or (targetY < 0): - yVelocity[i] = -1* yVelocity[i] - thisTileGrid.x=thisTileGrid.x + xVelocity[i] - thisTileGrid.y=thisTileGrid.y + yVelocity[i] - gc.collect() - - # Print the memory availability every 10 movements. - if charCount % 10 == 0: - print( 'Memory free: {}'.format(gc.mem_free()) ) - - - - - - - diff --git a/examples/textmap_simpletest.py b/examples/textmap_simpletest.py index 85252ca..e95736d 100644 --- a/examples/textmap_simpletest.py +++ b/examples/textmap_simpletest.py @@ -108,13 +108,13 @@ textBoxes=[] # list of textBox instances -textBoxes.append( textBox('', fontList[0], DISPLAY_WIDTH, DISPLAY_HEIGHT, backgroundColor=0x000000, textColor=0x443344) ) +textBoxes.append( textBox('', DISPLAY_WIDTH, DISPLAY_HEIGHT, 0x000000, 0x443344, fontList[0]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[0], 150, 60, backgroundColor=0x000000, textColor=0xFFFFFF) ) +textBoxes.append( textBox('', 150, 60, 0x000000, 0xFFFFFF, fontList[0]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[1], 160, 100, backgroundColor=0xFF00FF, textColor=0xFFFFFF) ) +textBoxes.append( textBox('', 160, 100, 0xFF00FF, 0xFFFFFF, fontList[1]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[2], 180, 80, backgroundColor=0x00FFFF, textColor=0x444444) ) +textBoxes.append( textBox('', 180, 80, 0x00FFFF, 0x444444, fontList[2]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) gc.collect() myGroup = displayio.Group( max_size=len(textBoxes) ) # Create a group for displaying diff --git a/examples/textmap_simpletest2.py b/examples/textmap_simpletest2.py deleted file mode 100755 index a9a7e83..0000000 --- a/examples/textmap_simpletest2.py +++ /dev/null @@ -1,91 +0,0 @@ -# Example that exercises the textMap library: https://github.com/kmatch98/CircuitPython_textMap -# - -import board -import displayio -import terminalio -import busio - -import textmap -from textmap import textBox - -from adafruit_bitmap_font import bitmap_font - -#from adafruit_st7789 import ST7789 -from adafruit_ili9341 import ILI9341 - -displayio.release_displays() - -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) - pass -spi.unlock() - -display_bus = displayio.FourWire( - spi, - command=tft_dc, - chip_select=tft_cs, - reset=tft_reset, - baudrate=32000000, - polarity=1, - phase=1, -) - -print('spi.frequency: {}'.format(spi.frequency)) - -DISPLAY_WIDTH=320 -DISPLAY_HEIGHT=240 - -#display = ST7789(display_bus, width=DISPLAY_WIDTH, height=DISPlAY_HEIGHT, rotation=0, rowstart=80, colstart=0) -display = ILI9341(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, rotation=180, auto_refresh=True) - -display.show(None) - - -myGroup = displayio.Group(max_size=10) # create a group to hold the text boxes - -# Make a background color fill that covers the whole display -color_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1) -color_palette = displayio.Palette(1) -color_palette[0] = 0x000000 # black - -display_background = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) -myGroup.append(display_background) -display.show(myGroup) - -print('Showing background bitmap.') - - -print('loading fonts...') - -font = terminalio.FONT # This is a fixed-width font of class "BuiltinFont" -fontText = bitmap_font.load_font('fonts/BitstreamVeraSans-Roman-24.bdf') -fontBold = bitmap_font.load_font('fonts/Helvetica-Bold-16.bdf') - -print('Fonts are loaded.') - -myText1 = textBox('This is a BuiltinFont with a\nnewline', font, 180, 40, backgroundColor=0x00FF00, textColor=0x000000) -myGroup.append(displayio.TileGrid(myText1.bitmap, pixel_shader=myText1.palette, x=30, y=5)) - -myText2 = textBox('BDF imported font', fontBold, 300, 40, backgroundColor=0xFF0000, textColor=0xFFFFFF) -myGroup.append(displayio.TileGrid(myText2.bitmap, pixel_shader=myText2.palette, x=5, y=60)) - -myText3 = textBox('Another BDF imported font', fontText, 280, 60, backgroundColor=0x0000FF, textColor=0xFFFFFF) -myGroup.append(displayio.TileGrid(myText3.bitmap, pixel_shader=myText3.palette, x=20, y=120)) - -myText4 = textBox('BuiltinFont hard wrapped text in this box, transparent background', font, 120, 100, backgroundColor=None, textColor=0x44FF44, lineSpacing=1.0) -myGroup.append(displayio.TileGrid(myText4.bitmap, pixel_shader=myText4.palette, x=80, y=150)) - -myText5 = textBox('BuiltinFont\ntextBox\ntransparent\nbackground', font, 120, 100, backgroundColor=None, textColor=0xFF00FF, lineSpacing=1.0) -myGroup.append(displayio.TileGrid(myText5.bitmap, pixel_shader=myText5.palette, x=230, y=150)) - -print('Printed text.') - -import time -time.sleep(100000) # delay for a long while diff --git a/examples/textmap_simpletest2_builtin_screen.py b/examples/textmap_simpletest2_builtin_screen.py deleted file mode 100755 index e944366..0000000 --- a/examples/textmap_simpletest2_builtin_screen.py +++ /dev/null @@ -1,66 +0,0 @@ -# Example that exercises the textMap library: https://github.com/kmatch98/CircuitPython_textMap -# - -import textmap -from textmap import textBox - -import board -import displayio -import time -import terminalio -import fontio -import sys -import busio - - -DISPLAY_WIDTH=320 -DISPLAY_HEIGHT=240 - -display = board.DISPLAY - -display.show(None) - -print ('Display is started') - - -myGroup = displayio.Group(max_size=10) # create a group to hold the text boxes - -# Make a background color fill that covers the whole display -color_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1) -color_palette = displayio.Palette(1) -color_palette[0] = 0x000000 # black - -display_background = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) -myGroup.append(display_background) -display.show(myGroup) - -print('Showing background bitmap.') - - -print('loading fonts...') - -font = terminalio.FONT # This is a fixed-width font of class "BuiltinFont" -fontText = bitmap_font.load_font('fonts/BitstreamVeraSans-Roman-24.bdf') -fontBold = bitmap_font.load_font('fonts/Helvetica-Bold-16.bdf') - -print('Fonts are loaded.') - -myText1 = textBox('This is a BuiltinFont with a\nnewline', font, 180, 40, backgroundColor=0x00FF00, textColor=0x000000) -myGroup.append(displayio.TileGrid(myText1.bitmap, pixel_shader=myText1.palette, x=30, y=5)) - -myText2 = textBox('BDF imported font', fontBold, 300, 40, backgroundColor=0xFF0000, textColor=0xFFFFFF) -myGroup.append(displayio.TileGrid(myText2.bitmap, pixel_shader=myText2.palette, x=5, y=60)) - -myText3 = textBox('Another BDF imported font', fontText, 280, 60, backgroundColor=0x0000FF, textColor=0xFFFFFF) -myGroup.append(displayio.TileGrid(myText3.bitmap, pixel_shader=myText3.palette, x=20, y=120)) - -myText4 = textBox('BuiltinFont hard wrapped text in this box, transparent background', font, 120, 100, backgroundColor=None, textColor=0x44FF44, lineSpacing=1.0) -myGroup.append(displayio.TileGrid(myText4.bitmap, pixel_shader=myText4.palette, x=80, y=150)) - -myText5 = textBox('BuiltinFont\ntextBox\ntransparent\nbackground', font, 120, 100, backgroundColor=None, textColor=0xFF00FF, lineSpacing=1.0) -myGroup.append(displayio.TileGrid(myText5.bitmap, pixel_shader=myText5.palette, x=230, y=150)) - -print('Printed text.') - -import time -time.sleep(100000) # delay for a long while diff --git a/examples/textmap_simpletest_builtin_screen.py b/examples/textmap_simpletest_builtin_screen.py index ce8f155..3e33d0a 100644 --- a/examples/textmap_simpletest_builtin_screen.py +++ b/examples/textmap_simpletest_builtin_screen.py @@ -26,7 +26,6 @@ print ('Display is started') - # load all the fonts print('loading fonts...') @@ -76,13 +75,13 @@ textBoxes=[] # list of textBox instances -textBoxes.append( textBox('', fontList[0], DISPLAY_WIDTH, DISPLAY_HEIGHT, backgroundColor=0x000000, textColor=0x443344) ) +textBoxes.append( textBox('', DISPLAY_WIDTH, DISPLAY_HEIGHT, 0x000000, 0x443344, fontList[0]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[0], 150, 60, backgroundColor=0x000000, textColor=0xFFFFFF) ) +textBoxes.append( textBox('', 150, 60, 0x000000, 0xFFFFFF, fontList[0]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[1], 160, 100, backgroundColor=0xFF00FF, textColor=0xFFFFFF) ) +textBoxes.append( textBox('', 160, 100, 0xFF00FF, 0xFFFFFF, fontList[1]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', fontList[2], 180, 80, backgroundColor=0x00FFFF, textColor=0x444444) ) +textBoxes.append( textBox('', 180, 80, 0x00FFFF, 0x444444, fontList[2]) ) print( 'Memory free: {}'.format(gc.mem_free()) ) gc.collect() myGroup = displayio.Group( max_size=len(textBoxes) ) # Create a group for displaying @@ -138,4 +137,4 @@ # Print the memory availability every 10 movements. if charCount % 10 == 0: - print( 'Memory free: {}'.format(gc.mem_free()) ) \ No newline at end of file + print( 'Memory free: {}'.format(gc.mem_free()) ) diff --git a/textmap.py b/textmap.py index dade49e..1fbc146 100644 --- a/textmap.py +++ b/textmap.py @@ -65,45 +65,38 @@ def bounding_box(text, font, lineSpacing, scale=1): # text terminal box, prior to actually printing the text in the bitmap. # # Note: Scale is not implemented at this time - - #print('bounding_box text: {}'.format(text)) boxHeight = boxWidth = 0 fontHeight = font.get_glyph(ord("M")).height for char in text: - if char != '': - #print('bounding_box char: {}'.format(char)) - myGlyph = font.get_glyph(ord(char)) - - width = myGlyph.width - height = myGlyph.height - dx = myGlyph.dx - dy = myGlyph.dy - shift_x = myGlyph.shift_x - shift_y = myGlyph.shift_x - - # Not working yet*** - # This offset is used to match the label.py function from Adafruit_Display_Text library - # y_offset = int( - # ( - # self._font.get_glyph(ord("M")).height - # - new_text.count("\n") * self.height * self.line_spacing - # ) - # / 2 ) - - # yOffset = int( (fontHeight-height*lineSpacing)/2 ) - yOffset = fontHeight - height - - boxWidth = boxWidth + shift_x - boxHeight = max(boxHeight, height - dy + yOffset) + myGlyph = font.get_glyph(ord(char)) + + width = myGlyph.width + height = myGlyph.height + dx = myGlyph.dx + dy = myGlyph.dy + shift_x = myGlyph.shift_x + shift_y = myGlyph.shift_x + + # Not working yet*** + # This offset is used to match the label.py function from Adafruit_Display_Text library + # y_offset = int( + # ( + # self._font.get_glyph(ord("M")).height + # - new_text.count("\n") * self.height * self.line_spacing + # ) + # / 2 ) + + # yOffset = int( (fontHeight-height*lineSpacing)/2 ) + yOffset = fontHeight - height + + boxWidth = boxWidth + shift_x + boxHeight = max(boxHeight, height - dy + yOffset) return (boxWidth, boxHeight) def placeText( - bitmap, text, font, lineSpacing, xPosition, yPosition, - textPaletteIndex=1, - backgroundPaletteIndex=0, - scale=1, + bitmap, text, font, lineSpacing, xPosition, yPosition, paletteIndex=1, scale=1 ): # placeText - Writes text into a bitmap at the specified location. # @@ -113,30 +106,20 @@ def placeText( # Verify paletteIndex is working properly with * operator, especially if accommodating multicolored fonts # # Note: Scale is not implemented at this time + import terminalio fontHeight = font.get_glyph(ord("M")).height - if font == terminalio.FONT: print("terminalio.FONT Found - BuiltinFont not handled by this function") # handle this differently else: + bitmapWidth = bitmap.width bitmapHeight = bitmap.height - if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background - # draw a bounding box where the text will go - - (ignore, fontLineHeight)=bounding_box('M g', font, lineSpacing, scale) # check height with ascender and descender. - (boxX, boxY) = bounding_box(text, font, lineSpacing, scale) - boxY=max(fontLineHeight, boxY) - for y in range(boxY): - for x in range(boxX): - if (xPosition+x < bitmapWidth) and (yPosition+y < bitmapHeight): # check boundaries - bitmap[xPosition+x, yPosition+y]=backgroundPaletteIndex - for char in text: myGlyph = font.get_glyph(ord(char)) @@ -174,20 +157,12 @@ def placeText( and (yPlacement < bitmapHeight) ): - paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) - #bitmap[xPlacement, yPlacement] = ( - # myGlyph.bitmap[x, y] * paletteIndex - #) - - # Allows for different paletteIndex for background and text. - bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x,y]] - - - + bitmap[xPlacement, yPlacement] = ( + myGlyph.bitmap[x, y] * paletteIndex + ) xPosition = xPosition + shift_x - return (xPosition, yPosition) @@ -269,7 +244,7 @@ def getCursor(self): # get current cursor position, tuple return (self._cursorX, self._cursorY) def clearBitmap(self): - self.bitmap.fill(0) # quick builtin bitmap fill operation + self.bitmap.fill(0) # quick builtin bitmap fill operation self.setCursor(self._startX, self._startY) if self._memorySaver == False: self._text='' # reset the text string From d913d0a5d4ccf850838341514d38bc0d813af8f2 Mon Sep 17 00:00:00 2001 From: kmatch98 <33587466+kmatch98@users.noreply.github.com> Date: Fri, 5 Jun 2020 17:13:56 -0500 Subject: [PATCH 2/7] Revert "Revert "Added background color capability."" --- examples/code.py | 180 ++++++++++++++++++ examples/textmap_simpletest.py | 8 +- examples/textmap_simpletest2.py | 91 +++++++++ .../textmap_simpletest2_builtin_screen.py | 66 +++++++ examples/textmap_simpletest_builtin_screen.py | 11 +- textmap.py | 85 ++++++--- 6 files changed, 402 insertions(+), 39 deletions(-) create mode 100644 examples/code.py create mode 100755 examples/textmap_simpletest2.py create mode 100755 examples/textmap_simpletest2_builtin_screen.py diff --git a/examples/code.py b/examples/code.py new file mode 100644 index 0000000..85252ca --- /dev/null +++ b/examples/code.py @@ -0,0 +1,180 @@ +# Sample code using the textMap library and the "textBox" wrapper class +# Creates four textBox instances +# Inserts each textBox into a tileGrid group +# Writes text into the box one character at a time +# Moves the position of the textBox around the display +# Clears each textBox after the full string is written (even if the text is outside of the box) + +import textmap +from textmap import textBox + +import board +import displayio +import time +import terminalio +import fontio +import sys +import busio +#from adafruit_st7789 import ST7789 +from adafruit_ili9341 import ILI9341 + +# Setup the SPI display + +print('Starting the display...') # goes to serial only +displayio.release_displays() + + +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) + pass +spi.unlock() + +display_bus = displayio.FourWire( + spi, + command=tft_dc, + chip_select=tft_cs, + reset=tft_reset, + baudrate=32000000, + polarity=1, + phase=1, +) + +print('spi.frequency: {}'.format(spi.frequency)) + +DISPLAY_WIDTH=320 +DISPLAY_HEIGHT=240 + +#display = ST7789(display_bus, width=240, height=240, rotation=0, rowstart=80, colstart=0) +display = ILI9341(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, rotation=180, auto_refresh=True) + +display.show(None) + +print ('Display is started') + + +# load all the fonts +print('loading fonts...') + +import terminalio + + +fontList = [] +fontHeight = [] + +##### the BuiltinFont terminalio.FONT has a different return strategy for get_glyphs and +# is currently not handled by these functions. +#fontList.append(terminalio.FONT) +#fontHeight = [10] # somehow the terminalio.FONT needs to be adjusted to 10 + +# Load some proportional fonts +fontFiles = [ + 'fonts/Helvetica-Bold-16.bdf', + 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2 + 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText + ] + +from adafruit_bitmap_font import bitmap_font + +for i, fontFile in enumerate(fontFiles): + thisFont = bitmap_font.load_font(fontFile) + fontList.append(thisFont) + fontHeight.append( thisFont.get_glyph(ord("M")).height ) + +preloadTheGlyphs= 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 preloadTheGlyphs: + + # identify the glyphs to load into memory -> increases rendering speed + glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! ' + + print('loading glyphs...') + for font in fontList: + font.load_glyphs(glyphs) + print('Glyphs are loaded.') + +print('Fonts completed loading.') + +# create group +import gc +gc.collect() +print( 'Memory free: {}'.format(gc.mem_free()) ) + +textBoxes=[] # list of textBox instances + +textBoxes.append( textBox('', fontList[0], DISPLAY_WIDTH, DISPLAY_HEIGHT, backgroundColor=0x000000, textColor=0x443344) ) +print( 'Memory free: {}'.format(gc.mem_free()) ) +textBoxes.append( textBox('', fontList[0], 150, 60, backgroundColor=0x000000, textColor=0xFFFFFF) ) +print( 'Memory free: {}'.format(gc.mem_free()) ) +textBoxes.append( textBox('', fontList[1], 160, 100, backgroundColor=0xFF00FF, textColor=0xFFFFFF) ) +print( 'Memory free: {}'.format(gc.mem_free()) ) +textBoxes.append( textBox('', fontList[2], 180, 80, backgroundColor=0x00FFFF, textColor=0x444444) ) +print( 'Memory free: {}'.format(gc.mem_free()) ) +gc.collect() +myGroup = displayio.Group( max_size=len(textBoxes) ) # Create a group for displaying + +tileGridList=[] # list of tileGrids + + +#startPositions +x=[0, 10, 160, 50] +y=[0, 20, 80, 150] + +xVelocity=[0, 1, -1, 2] +yVelocity=[0, -1, 2, 1] + +gc.collect() +stringList=[] +stringList.append('Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box. Full Screen Size: This is a stationary box, not a stationery box.') +stringList.append('Helvetica Bold 16 font - with Black background color') +stringList.append('Vera Sans 24 font - this is a longer line that is wrapping around') +stringList.append('Vera Sans 16 font - how much text will this hold but it will not print text that goes outside the box but it will cut it off at the bottom if it is too large.') + +for i, box in enumerate(textBoxes): + tileGridList.append (displayio.TileGrid(box.bitmap, pixel_shader=box.palette, x=x[i], y=y[i]) ) + myGroup.append(tileGridList[i]) +display.show(myGroup) + +charCount=0 +while True: + + # Add characters one at a time. + + for i, box in enumerate(textBoxes): + charToPrint=charCount % len(stringList[i]) + if charToPrint == 0: + box.clearBitmap() + box.addText(stringList[i][charToPrint]) # add a character + gc.collect() + + charCount += 1 + + # Move each box + + for i, thisTileGrid in enumerate(tileGridList): + targetX=thisTileGrid.x + xVelocity[i] + targetY=thisTileGrid.y + yVelocity[i] + if ( (targetX + textBoxes[i].bitmap.width) >= DISPLAY_WIDTH ) or (targetX < 0): + xVelocity[i] = -1* xVelocity[i] + if ( (targetY + textBoxes[i].bitmap.height) >= DISPLAY_HEIGHT ) or (targetY < 0): + yVelocity[i] = -1* yVelocity[i] + thisTileGrid.x=thisTileGrid.x + xVelocity[i] + thisTileGrid.y=thisTileGrid.y + yVelocity[i] + gc.collect() + + # Print the memory availability every 10 movements. + if charCount % 10 == 0: + print( 'Memory free: {}'.format(gc.mem_free()) ) + + + + + + + diff --git a/examples/textmap_simpletest.py b/examples/textmap_simpletest.py index e95736d..85252ca 100644 --- a/examples/textmap_simpletest.py +++ b/examples/textmap_simpletest.py @@ -108,13 +108,13 @@ textBoxes=[] # list of textBox instances -textBoxes.append( textBox('', DISPLAY_WIDTH, DISPLAY_HEIGHT, 0x000000, 0x443344, fontList[0]) ) +textBoxes.append( textBox('', fontList[0], DISPLAY_WIDTH, DISPLAY_HEIGHT, backgroundColor=0x000000, textColor=0x443344) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', 150, 60, 0x000000, 0xFFFFFF, fontList[0]) ) +textBoxes.append( textBox('', fontList[0], 150, 60, backgroundColor=0x000000, textColor=0xFFFFFF) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', 160, 100, 0xFF00FF, 0xFFFFFF, fontList[1]) ) +textBoxes.append( textBox('', fontList[1], 160, 100, backgroundColor=0xFF00FF, textColor=0xFFFFFF) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', 180, 80, 0x00FFFF, 0x444444, fontList[2]) ) +textBoxes.append( textBox('', fontList[2], 180, 80, backgroundColor=0x00FFFF, textColor=0x444444) ) print( 'Memory free: {}'.format(gc.mem_free()) ) gc.collect() myGroup = displayio.Group( max_size=len(textBoxes) ) # Create a group for displaying diff --git a/examples/textmap_simpletest2.py b/examples/textmap_simpletest2.py new file mode 100755 index 0000000..a9a7e83 --- /dev/null +++ b/examples/textmap_simpletest2.py @@ -0,0 +1,91 @@ +# Example that exercises the textMap library: https://github.com/kmatch98/CircuitPython_textMap +# + +import board +import displayio +import terminalio +import busio + +import textmap +from textmap import textBox + +from adafruit_bitmap_font import bitmap_font + +#from adafruit_st7789 import ST7789 +from adafruit_ili9341 import ILI9341 + +displayio.release_displays() + +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) + pass +spi.unlock() + +display_bus = displayio.FourWire( + spi, + command=tft_dc, + chip_select=tft_cs, + reset=tft_reset, + baudrate=32000000, + polarity=1, + phase=1, +) + +print('spi.frequency: {}'.format(spi.frequency)) + +DISPLAY_WIDTH=320 +DISPLAY_HEIGHT=240 + +#display = ST7789(display_bus, width=DISPLAY_WIDTH, height=DISPlAY_HEIGHT, rotation=0, rowstart=80, colstart=0) +display = ILI9341(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, rotation=180, auto_refresh=True) + +display.show(None) + + +myGroup = displayio.Group(max_size=10) # create a group to hold the text boxes + +# Make a background color fill that covers the whole display +color_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1) +color_palette = displayio.Palette(1) +color_palette[0] = 0x000000 # black + +display_background = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) +myGroup.append(display_background) +display.show(myGroup) + +print('Showing background bitmap.') + + +print('loading fonts...') + +font = terminalio.FONT # This is a fixed-width font of class "BuiltinFont" +fontText = bitmap_font.load_font('fonts/BitstreamVeraSans-Roman-24.bdf') +fontBold = bitmap_font.load_font('fonts/Helvetica-Bold-16.bdf') + +print('Fonts are loaded.') + +myText1 = textBox('This is a BuiltinFont with a\nnewline', font, 180, 40, backgroundColor=0x00FF00, textColor=0x000000) +myGroup.append(displayio.TileGrid(myText1.bitmap, pixel_shader=myText1.palette, x=30, y=5)) + +myText2 = textBox('BDF imported font', fontBold, 300, 40, backgroundColor=0xFF0000, textColor=0xFFFFFF) +myGroup.append(displayio.TileGrid(myText2.bitmap, pixel_shader=myText2.palette, x=5, y=60)) + +myText3 = textBox('Another BDF imported font', fontText, 280, 60, backgroundColor=0x0000FF, textColor=0xFFFFFF) +myGroup.append(displayio.TileGrid(myText3.bitmap, pixel_shader=myText3.palette, x=20, y=120)) + +myText4 = textBox('BuiltinFont hard wrapped text in this box, transparent background', font, 120, 100, backgroundColor=None, textColor=0x44FF44, lineSpacing=1.0) +myGroup.append(displayio.TileGrid(myText4.bitmap, pixel_shader=myText4.palette, x=80, y=150)) + +myText5 = textBox('BuiltinFont\ntextBox\ntransparent\nbackground', font, 120, 100, backgroundColor=None, textColor=0xFF00FF, lineSpacing=1.0) +myGroup.append(displayio.TileGrid(myText5.bitmap, pixel_shader=myText5.palette, x=230, y=150)) + +print('Printed text.') + +import time +time.sleep(100000) # delay for a long while diff --git a/examples/textmap_simpletest2_builtin_screen.py b/examples/textmap_simpletest2_builtin_screen.py new file mode 100755 index 0000000..e944366 --- /dev/null +++ b/examples/textmap_simpletest2_builtin_screen.py @@ -0,0 +1,66 @@ +# Example that exercises the textMap library: https://github.com/kmatch98/CircuitPython_textMap +# + +import textmap +from textmap import textBox + +import board +import displayio +import time +import terminalio +import fontio +import sys +import busio + + +DISPLAY_WIDTH=320 +DISPLAY_HEIGHT=240 + +display = board.DISPLAY + +display.show(None) + +print ('Display is started') + + +myGroup = displayio.Group(max_size=10) # create a group to hold the text boxes + +# Make a background color fill that covers the whole display +color_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1) +color_palette = displayio.Palette(1) +color_palette[0] = 0x000000 # black + +display_background = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) +myGroup.append(display_background) +display.show(myGroup) + +print('Showing background bitmap.') + + +print('loading fonts...') + +font = terminalio.FONT # This is a fixed-width font of class "BuiltinFont" +fontText = bitmap_font.load_font('fonts/BitstreamVeraSans-Roman-24.bdf') +fontBold = bitmap_font.load_font('fonts/Helvetica-Bold-16.bdf') + +print('Fonts are loaded.') + +myText1 = textBox('This is a BuiltinFont with a\nnewline', font, 180, 40, backgroundColor=0x00FF00, textColor=0x000000) +myGroup.append(displayio.TileGrid(myText1.bitmap, pixel_shader=myText1.palette, x=30, y=5)) + +myText2 = textBox('BDF imported font', fontBold, 300, 40, backgroundColor=0xFF0000, textColor=0xFFFFFF) +myGroup.append(displayio.TileGrid(myText2.bitmap, pixel_shader=myText2.palette, x=5, y=60)) + +myText3 = textBox('Another BDF imported font', fontText, 280, 60, backgroundColor=0x0000FF, textColor=0xFFFFFF) +myGroup.append(displayio.TileGrid(myText3.bitmap, pixel_shader=myText3.palette, x=20, y=120)) + +myText4 = textBox('BuiltinFont hard wrapped text in this box, transparent background', font, 120, 100, backgroundColor=None, textColor=0x44FF44, lineSpacing=1.0) +myGroup.append(displayio.TileGrid(myText4.bitmap, pixel_shader=myText4.palette, x=80, y=150)) + +myText5 = textBox('BuiltinFont\ntextBox\ntransparent\nbackground', font, 120, 100, backgroundColor=None, textColor=0xFF00FF, lineSpacing=1.0) +myGroup.append(displayio.TileGrid(myText5.bitmap, pixel_shader=myText5.palette, x=230, y=150)) + +print('Printed text.') + +import time +time.sleep(100000) # delay for a long while diff --git a/examples/textmap_simpletest_builtin_screen.py b/examples/textmap_simpletest_builtin_screen.py index 3e33d0a..ce8f155 100644 --- a/examples/textmap_simpletest_builtin_screen.py +++ b/examples/textmap_simpletest_builtin_screen.py @@ -26,6 +26,7 @@ print ('Display is started') + # load all the fonts print('loading fonts...') @@ -75,13 +76,13 @@ textBoxes=[] # list of textBox instances -textBoxes.append( textBox('', DISPLAY_WIDTH, DISPLAY_HEIGHT, 0x000000, 0x443344, fontList[0]) ) +textBoxes.append( textBox('', fontList[0], DISPLAY_WIDTH, DISPLAY_HEIGHT, backgroundColor=0x000000, textColor=0x443344) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', 150, 60, 0x000000, 0xFFFFFF, fontList[0]) ) +textBoxes.append( textBox('', fontList[0], 150, 60, backgroundColor=0x000000, textColor=0xFFFFFF) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', 160, 100, 0xFF00FF, 0xFFFFFF, fontList[1]) ) +textBoxes.append( textBox('', fontList[1], 160, 100, backgroundColor=0xFF00FF, textColor=0xFFFFFF) ) print( 'Memory free: {}'.format(gc.mem_free()) ) -textBoxes.append( textBox('', 180, 80, 0x00FFFF, 0x444444, fontList[2]) ) +textBoxes.append( textBox('', fontList[2], 180, 80, backgroundColor=0x00FFFF, textColor=0x444444) ) print( 'Memory free: {}'.format(gc.mem_free()) ) gc.collect() myGroup = displayio.Group( max_size=len(textBoxes) ) # Create a group for displaying @@ -137,4 +138,4 @@ # Print the memory availability every 10 movements. if charCount % 10 == 0: - print( 'Memory free: {}'.format(gc.mem_free()) ) + print( 'Memory free: {}'.format(gc.mem_free()) ) \ No newline at end of file diff --git a/textmap.py b/textmap.py index 1fbc146..dade49e 100644 --- a/textmap.py +++ b/textmap.py @@ -65,38 +65,45 @@ def bounding_box(text, font, lineSpacing, scale=1): # text terminal box, prior to actually printing the text in the bitmap. # # Note: Scale is not implemented at this time + + #print('bounding_box text: {}'.format(text)) boxHeight = boxWidth = 0 fontHeight = font.get_glyph(ord("M")).height for char in text: - myGlyph = font.get_glyph(ord(char)) - - width = myGlyph.width - height = myGlyph.height - dx = myGlyph.dx - dy = myGlyph.dy - shift_x = myGlyph.shift_x - shift_y = myGlyph.shift_x - - # Not working yet*** - # This offset is used to match the label.py function from Adafruit_Display_Text library - # y_offset = int( - # ( - # self._font.get_glyph(ord("M")).height - # - new_text.count("\n") * self.height * self.line_spacing - # ) - # / 2 ) - - # yOffset = int( (fontHeight-height*lineSpacing)/2 ) - yOffset = fontHeight - height - - boxWidth = boxWidth + shift_x - boxHeight = max(boxHeight, height - dy + yOffset) + if char != '': + #print('bounding_box char: {}'.format(char)) + myGlyph = font.get_glyph(ord(char)) + + width = myGlyph.width + height = myGlyph.height + dx = myGlyph.dx + dy = myGlyph.dy + shift_x = myGlyph.shift_x + shift_y = myGlyph.shift_x + + # Not working yet*** + # This offset is used to match the label.py function from Adafruit_Display_Text library + # y_offset = int( + # ( + # self._font.get_glyph(ord("M")).height + # - new_text.count("\n") * self.height * self.line_spacing + # ) + # / 2 ) + + # yOffset = int( (fontHeight-height*lineSpacing)/2 ) + yOffset = fontHeight - height + + boxWidth = boxWidth + shift_x + boxHeight = max(boxHeight, height - dy + yOffset) return (boxWidth, boxHeight) def placeText( - bitmap, text, font, lineSpacing, xPosition, yPosition, paletteIndex=1, scale=1 + bitmap, text, font, lineSpacing, xPosition, yPosition, + textPaletteIndex=1, + backgroundPaletteIndex=0, + scale=1, ): # placeText - Writes text into a bitmap at the specified location. # @@ -106,20 +113,30 @@ def placeText( # Verify paletteIndex is working properly with * operator, especially if accommodating multicolored fonts # # Note: Scale is not implemented at this time - import terminalio fontHeight = font.get_glyph(ord("M")).height + if font == terminalio.FONT: print("terminalio.FONT Found - BuiltinFont not handled by this function") # handle this differently else: - bitmapWidth = bitmap.width bitmapHeight = bitmap.height + if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background + # draw a bounding box where the text will go + + (ignore, fontLineHeight)=bounding_box('M g', font, lineSpacing, scale) # check height with ascender and descender. + (boxX, boxY) = bounding_box(text, font, lineSpacing, scale) + boxY=max(fontLineHeight, boxY) + for y in range(boxY): + for x in range(boxX): + if (xPosition+x < bitmapWidth) and (yPosition+y < bitmapHeight): # check boundaries + bitmap[xPosition+x, yPosition+y]=backgroundPaletteIndex + for char in text: myGlyph = font.get_glyph(ord(char)) @@ -157,12 +174,20 @@ def placeText( and (yPlacement < bitmapHeight) ): + paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) - bitmap[xPlacement, yPlacement] = ( - myGlyph.bitmap[x, y] * paletteIndex - ) + #bitmap[xPlacement, yPlacement] = ( + # myGlyph.bitmap[x, y] * paletteIndex + #) + + # Allows for different paletteIndex for background and text. + bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x,y]] + + + xPosition = xPosition + shift_x + return (xPosition, yPosition) @@ -244,7 +269,7 @@ def getCursor(self): # get current cursor position, tuple return (self._cursorX, self._cursorY) def clearBitmap(self): - self.bitmap.fill(0) # quick builtin bitmap fill operation + self.bitmap.fill(0) # quick builtin bitmap fill operation self.setCursor(self._startX, self._startY) if self._memorySaver == False: self._text='' # reset the text string From 068ddae8fb5bc3cf30699ff6167f21da45b16194 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Fri, 5 Jun 2020 17:28:56 -0500 Subject: [PATCH 3/7] Updating to accommodate built-in font --- textmap.py | 138 +++++++++++++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/textmap.py b/textmap.py index dade49e..6f5612c 100644 --- a/textmap.py +++ b/textmap.py @@ -71,10 +71,10 @@ def bounding_box(text, font, lineSpacing, scale=1): fontHeight = font.get_glyph(ord("M")).height for char in text: - if char != '': - #print('bounding_box char: {}'.format(char)) - myGlyph = font.get_glyph(ord(char)) - + myGlyph = font.get_glyph(ord(char)) + if myGlyph == None: # Error checking: no glyph found + print('Glyph not found: {}'.format(repr(char))) + else: width = myGlyph.width height = myGlyph.height dx = myGlyph.dx @@ -117,75 +117,77 @@ def placeText( fontHeight = font.get_glyph(ord("M")).height + bitmapWidth = bitmap.width + bitmapHeight = bitmap.height - if font == terminalio.FONT: - print("terminalio.FONT Found - BuiltinFont not handled by this function") - # handle this differently - - else: - bitmapWidth = bitmap.width - bitmapHeight = bitmap.height - - if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background - # draw a bounding box where the text will go - (ignore, fontLineHeight)=bounding_box('M g', font, lineSpacing, scale) # check height with ascender and descender. - (boxX, boxY) = bounding_box(text, font, lineSpacing, scale) - boxY=max(fontLineHeight, boxY) - for y in range(boxY): - for x in range(boxX): - if (xPosition+x < bitmapWidth) and (yPosition+y < bitmapHeight): # check boundaries - bitmap[xPosition+x, yPosition+y]=backgroundPaletteIndex - - for char in text: - myGlyph = font.get_glyph(ord(char)) - - width = myGlyph.width - height = myGlyph.height - # print('glyph width: {}, height: {}'.format(width, height)) - dx = myGlyph.dx - dy = myGlyph.dy - shift_x = myGlyph.shift_x - shift_y = myGlyph.shift_x - # Not working yet*** - # This offset is used to match the label.py function from Adafruit_Display_Text library - # y_offset = int( - # ( - # self._font.get_glyph(ord("M")).height - # - new_text.count("\n") * self.height * self.line_spacing - # ) - # / 2 ) + if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background + # draw a bounding box where the text will go - # position_y = y - glyph.height - glyph.dy + y_offset + (ignore, fontLineHeight)=bounding_box('M g', font, lineSpacing, scale) # check height with ascender and descender. + (boxX, boxY) = bounding_box(text, font, lineSpacing, scale) + boxY=max(fontLineHeight, boxY) + for y in range(boxY): + for x in range(boxX): + if (xPosition+x < bitmapWidth) and (yPosition+y < bitmapHeight): # check boundaries + bitmap[xPosition+x, yPosition+y]=backgroundPaletteIndex - # yOffset = int( (fontHeight-height*lineSpacing)/2 ) - yOffset = fontHeight - height - for y in range(height): - for x in range(width): - xPlacement = x + xPosition + dx - # yPlacement=y+yPosition-height-dy+yOffset - yPlacement = y + yPosition - dy + yOffset - - if ( - (xPlacement >= 0) - and (yPlacement >= 0) - and (xPlacement < bitmapWidth) - and (yPlacement < bitmapHeight) - ): - - paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) - # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) - #bitmap[xPlacement, yPlacement] = ( - # myGlyph.bitmap[x, y] * paletteIndex - #) - - # Allows for different paletteIndex for background and text. - bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x,y]] - - - - xPosition = xPosition + shift_x + for char in text: + myGlyph = font.get_glyph(ord(char)) + + if myGlyph == None: # Error checking: no glyph found + print('Glyph not found: {}'.format(repr(char))) + else: + + width = myGlyph.width + height = myGlyph.height + # print('glyph width: {}, height: {}'.format(width, height)) + dx = myGlyph.dx + dy = myGlyph.dy + shift_x = myGlyph.shift_x + shift_y = myGlyph.shift_x + glyph_offset_x = myGlyph.tile_index * width # for type BuiltinFont, this creates the x-offset in the glyph bitmap. + # for BDF loaded fonts, this should equal 0 + + # Not working yet*** + # This offset is used to match the label.py function from Adafruit_Display_Text library + # y_offset = int( + # ( + # self._font.get_glyph(ord("M")).height + # - new_text.count("\n") * self.height * self.line_spacing + # ) + # / 2 ) + + # position_y = y - glyph.height - glyph.dy + y_offset + + # yOffset = int( (fontHeight-height*lineSpacing)/2 ) + yOffset = fontHeight - height + for y in range(height): + for x in range(width): + xPlacement = x + xPosition + dx + # yPlacement=y+yPosition-height-dy+yOffset + yPlacement = y + yPosition - dy + yOffset + + if ( + (xPlacement >= 0) + and (yPlacement >= 0) + and (xPlacement < bitmapWidth) + and (yPlacement < bitmapHeight) + ): + + paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) + # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) + #bitmap[xPlacement, yPlacement] = ( + # myGlyph.bitmap[x, y] * paletteIndex + #) + + # Allows for different paletteIndex for background and text. + bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]] + + + + xPosition = xPosition + shift_x return (xPosition, yPosition) From 4ddf716fcf6027d2eb3dfaa5cdd5a9142d7c2f5a Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Fri, 5 Jun 2020 17:33:14 -0500 Subject: [PATCH 4/7] Updated typo indention --- textmap.py | 96 +++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/textmap.py b/textmap.py index 6f5612c..a127943 100644 --- a/textmap.py +++ b/textmap.py @@ -140,54 +140,54 @@ def placeText( print('Glyph not found: {}'.format(repr(char))) else: - width = myGlyph.width - height = myGlyph.height - # print('glyph width: {}, height: {}'.format(width, height)) - dx = myGlyph.dx - dy = myGlyph.dy - shift_x = myGlyph.shift_x - shift_y = myGlyph.shift_x - glyph_offset_x = myGlyph.tile_index * width # for type BuiltinFont, this creates the x-offset in the glyph bitmap. - # for BDF loaded fonts, this should equal 0 - - # Not working yet*** - # This offset is used to match the label.py function from Adafruit_Display_Text library - # y_offset = int( - # ( - # self._font.get_glyph(ord("M")).height - # - new_text.count("\n") * self.height * self.line_spacing - # ) - # / 2 ) - - # position_y = y - glyph.height - glyph.dy + y_offset - - # yOffset = int( (fontHeight-height*lineSpacing)/2 ) - yOffset = fontHeight - height - for y in range(height): - for x in range(width): - xPlacement = x + xPosition + dx - # yPlacement=y+yPosition-height-dy+yOffset - yPlacement = y + yPosition - dy + yOffset - - if ( - (xPlacement >= 0) - and (yPlacement >= 0) - and (xPlacement < bitmapWidth) - and (yPlacement < bitmapHeight) - ): - - paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) - # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) - #bitmap[xPlacement, yPlacement] = ( - # myGlyph.bitmap[x, y] * paletteIndex - #) - - # Allows for different paletteIndex for background and text. - bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]] - - - - xPosition = xPosition + shift_x + width = myGlyph.width + height = myGlyph.height + # print('glyph width: {}, height: {}'.format(width, height)) + dx = myGlyph.dx + dy = myGlyph.dy + shift_x = myGlyph.shift_x + shift_y = myGlyph.shift_x + glyph_offset_x = myGlyph.tile_index * width # for type BuiltinFont, this creates the x-offset in the glyph bitmap. + # for BDF loaded fonts, this should equal 0 + + # Not working yet*** + # This offset is used to match the label.py function from Adafruit_Display_Text library + # y_offset = int( + # ( + # self._font.get_glyph(ord("M")).height + # - new_text.count("\n") * self.height * self.line_spacing + # ) + # / 2 ) + + # position_y = y - glyph.height - glyph.dy + y_offset + + # yOffset = int( (fontHeight-height*lineSpacing)/2 ) + yOffset = fontHeight - height + for y in range(height): + for x in range(width): + xPlacement = x + xPosition + dx + # yPlacement=y+yPosition-height-dy+yOffset + yPlacement = y + yPosition - dy + yOffset + + if ( + (xPlacement >= 0) + and (yPlacement >= 0) + and (xPlacement < bitmapWidth) + and (yPlacement < bitmapHeight) + ): + + paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) + # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) + #bitmap[xPlacement, yPlacement] = ( + # myGlyph.bitmap[x, y] * paletteIndex + #) + + # Allows for different paletteIndex for background and text. + bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]] + + + + xPosition = xPosition + shift_x return (xPosition, yPosition) From cbfbb0fc45eada263f3f89225c00cdc3f1feb681 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Wed, 10 Jun 2020 17:11:30 -0500 Subject: [PATCH 5/7] Added ~20percent speedup for glyph copy into the bitmap using direct indexing rather than x,y --- textmap.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) mode change 100644 => 100755 textmap.py diff --git a/textmap.py b/textmap.py old mode 100644 new mode 100755 index a127943..770f5f3 --- a/textmap.py +++ b/textmap.py @@ -128,10 +128,12 @@ def placeText( (ignore, fontLineHeight)=bounding_box('M g', font, lineSpacing, scale) # check height with ascender and descender. (boxX, boxY) = bounding_box(text, font, lineSpacing, scale) boxY=max(fontLineHeight, boxY) + for y in range(boxY): for x in range(boxX): if (xPosition+x < bitmapWidth) and (yPosition+y < bitmapHeight): # check boundaries - bitmap[xPosition+x, yPosition+y]=backgroundPaletteIndex + #bitmap[xPosition+x, yPosition+y]=backgroundPaletteIndex + bitmap[(yPosition+y)*bitmapWidth + (xPosition + x)]=backgroundPaletteIndex for char in text: myGlyph = font.get_glyph(ord(char)) @@ -183,7 +185,8 @@ def placeText( #) # Allows for different paletteIndex for background and text. - bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]] + #bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]] + bitmap[yPlacement*bitmapWidth + xPlacement] = paletteIndexes[myGlyph.bitmap[y*width + x+glyph_offset_x]] From 674d0b9377ea4ad98abb2cdf8be24945705ab47f Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 11 Jun 2020 13:57:09 -0500 Subject: [PATCH 6/7] updated textBox to include defaults for colors --- textmap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/textmap.py b/textmap.py index 770f5f3..7cb22a9 100755 --- a/textmap.py +++ b/textmap.py @@ -198,7 +198,7 @@ def placeText( class textBox: def __init__( - self, text, width, height, backgroundColor, textColor, font, lineSpacing=1.25 + self, text, font, width, height, backgroundColor=0x000000, textColor=0xFFFFFF, lineSpacing=1.25 ): import displayio From ab41a73ca01526ec1a9932036bac61a23f1917fe Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Thu, 23 Jul 2020 13:16:20 -0500 Subject: [PATCH 7/7] TBD --- textmap.py | 181 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 103 insertions(+), 78 deletions(-) diff --git a/textmap.py b/textmap.py index 7cb22a9..c628973 100755 --- a/textmap.py +++ b/textmap.py @@ -69,33 +69,43 @@ def bounding_box(text, font, lineSpacing, scale=1): #print('bounding_box text: {}'.format(text)) boxHeight = boxWidth = 0 fontHeight = font.get_glyph(ord("M")).height + thisLineWidth = 0 for char in text: - myGlyph = font.get_glyph(ord(char)) - if myGlyph == None: # Error checking: no glyph found - print('Glyph not found: {}'.format(repr(char))) - else: - width = myGlyph.width - height = myGlyph.height - dx = myGlyph.dx - dy = myGlyph.dy - shift_x = myGlyph.shift_x - shift_y = myGlyph.shift_x - - # Not working yet*** - # This offset is used to match the label.py function from Adafruit_Display_Text library - # y_offset = int( - # ( - # self._font.get_glyph(ord("M")).height - # - new_text.count("\n") * self.height * self.line_spacing - # ) - # / 2 ) - - # yOffset = int( (fontHeight-height*lineSpacing)/2 ) - yOffset = fontHeight - height - - boxWidth = boxWidth + shift_x - boxHeight = max(boxHeight, height - dy + yOffset) + if char == '\n': # newline + boxWidth = max(boxWidth, thisLineWidth) # check to see if the last line is wider than any others. + thisLineWidth = 0 # new line, so restart thislineWidth at 0 + boxHeight = boxHeight + lineSpacingY(font, lineSpacing, scale) # add a lineSpacing to the boxHeight + + else: + myGlyph = font.get_glyph(ord(char)) + if myGlyph == None: # Error checking: no glyph found + print('Glyph not found: {}'.format(repr(char))) + else: + width = myGlyph.width + height = myGlyph.height + dx = myGlyph.dx + dy = myGlyph.dy + shift_x = myGlyph.shift_x + shift_y = myGlyph.shift_x + + # Not working yet*** + # This offset is used to match the label.py function from Adafruit_Display_Text library + # y_offset = int( + # ( + # self._font.get_glyph(ord("M")).height + # - new_text.count("\n") * self.height * self.line_spacing + # ) + # / 2 ) + + # yOffset = int( (fontHeight-height*lineSpacing)/2 ) + yOffset = fontHeight - height + + thisLineWidth = thisLineWidth + shift_x + boxHeight = max(boxHeight, height - dy + yOffset) + + boxWidth = max(boxWidth, thisLineWidth) + return (boxWidth, boxHeight) @@ -103,7 +113,9 @@ def placeText( bitmap, text, font, lineSpacing, xPosition, yPosition, textPaletteIndex=1, backgroundPaletteIndex=0, - scale=1, + scale=1, + printOnlyPixels=True, # only update the bitmap where the glyph pixel color is > 0 + # this is especially useful for script fonts ): # placeText - Writes text into a bitmap at the specified location. # @@ -120,6 +132,7 @@ def placeText( bitmapWidth = bitmap.width bitmapHeight = bitmap.height + xStart=xPosition # starting x position (left margin) if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background @@ -136,61 +149,73 @@ def placeText( bitmap[(yPosition+y)*bitmapWidth + (xPosition + x)]=backgroundPaletteIndex for char in text: - myGlyph = font.get_glyph(ord(char)) - if myGlyph == None: # Error checking: no glyph found - print('Glyph not found: {}'.format(repr(char))) + if char == '\n': # newline + xPosition=xStart # reset to left column + yPosition = yPosition + lineSpacingY(font, lineSpacing, scale) # Add a newline + else: - width = myGlyph.width - height = myGlyph.height - # print('glyph width: {}, height: {}'.format(width, height)) - dx = myGlyph.dx - dy = myGlyph.dy - shift_x = myGlyph.shift_x - shift_y = myGlyph.shift_x - glyph_offset_x = myGlyph.tile_index * width # for type BuiltinFont, this creates the x-offset in the glyph bitmap. - # for BDF loaded fonts, this should equal 0 - - # Not working yet*** - # This offset is used to match the label.py function from Adafruit_Display_Text library - # y_offset = int( - # ( - # self._font.get_glyph(ord("M")).height - # - new_text.count("\n") * self.height * self.line_spacing - # ) - # / 2 ) - - # position_y = y - glyph.height - glyph.dy + y_offset - - # yOffset = int( (fontHeight-height*lineSpacing)/2 ) - yOffset = fontHeight - height - for y in range(height): - for x in range(width): - xPlacement = x + xPosition + dx - # yPlacement=y+yPosition-height-dy+yOffset - yPlacement = y + yPosition - dy + yOffset - - if ( - (xPlacement >= 0) - and (yPlacement >= 0) - and (xPlacement < bitmapWidth) - and (yPlacement < bitmapHeight) - ): - - paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) - # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) - #bitmap[xPlacement, yPlacement] = ( - # myGlyph.bitmap[x, y] * paletteIndex - #) - - # Allows for different paletteIndex for background and text. - #bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]] - bitmap[yPlacement*bitmapWidth + xPlacement] = paletteIndexes[myGlyph.bitmap[y*width + x+glyph_offset_x]] - - - - xPosition = xPosition + shift_x + myGlyph = font.get_glyph(ord(char)) + + if myGlyph == None: # Error checking: no glyph found + print('Glyph not found: {}'.format(repr(char))) + else: + + width = myGlyph.width + height = myGlyph.height + # print('glyph width: {}, height: {}'.format(width, height)) + dx = myGlyph.dx + dy = myGlyph.dy + shift_x = myGlyph.shift_x + shift_y = myGlyph.shift_x + glyph_offset_x = myGlyph.tile_index * width # for type BuiltinFont, this creates the x-offset in the glyph bitmap. + # for BDF loaded fonts, this should equal 0 + + # Not working yet*** + # This offset is used to match the label.py function from Adafruit_Display_Text library + # y_offset = int( + # ( + # self._font.get_glyph(ord("M")).height + # - new_text.count("\n") * self.height * self.line_spacing + # ) + # / 2 ) + + # position_y = y - glyph.height - glyph.dy + y_offset + + # yOffset = int( (fontHeight-height*lineSpacing)/2 ) + yOffset = fontHeight - height + for y in range(height): + for x in range(width): + xPlacement = x + xPosition + dx + # yPlacement=y+yPosition-height-dy+yOffset + yPlacement = y + yPosition - dy + yOffset + + if ( + (xPlacement >= 0) + and (yPlacement >= 0) + and (xPlacement < bitmapWidth) + and (yPlacement < bitmapHeight) + ): + + paletteIndexes=(backgroundPaletteIndex, textPaletteIndex) + # print('x: {}, y: {}, value: {}'.format(xPlacement, yPlacement, myGlyph.bitmap[x,y])) + #bitmap[xPlacement, yPlacement] = ( + # myGlyph.bitmap[x, y] * paletteIndex + #) + + # Allows for different paletteIndex for background and text. + #bitmap[xPlacement, yPlacement] = paletteIndexes[myGlyph.bitmap[x+glyph_offset_x,y]] + + thisPixelColor=paletteIndexes[myGlyph.bitmap[y*width + x+glyph_offset_x]] + if not printOnlyPixels or thisPixelColor > 0: + # write all characters if printOnlyPixels = False, or if thisPixelColor is 1 + bitmap[yPlacement*bitmapWidth + xPlacement] = thisPixelColor + elif (yPlacement > bitmapHeight): + break + + + xPosition = xPosition + shift_x return (xPosition, yPosition)