diff --git a/textmap.py b/textmap.py old mode 100644 new mode 100755 index dade49e..c628973 --- 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: - if char != '': - #print('bounding_box char: {}'.format(char)) + 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) - 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) @@ -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. # @@ -117,75 +129,93 @@ def placeText( fontHeight = font.get_glyph(ord("M")).height + bitmapWidth = bitmap.width + bitmapHeight = bitmap.height + + xStart=xPosition # starting x position (left margin) + - if font == terminalio.FONT: - print("terminalio.FONT Found - BuiltinFont not handled by this function") - # handle this differently + if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background + # draw a bounding box where the text will go - else: - bitmapWidth = bitmap.width - bitmapHeight = bitmap.height + (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[(yPosition+y)*bitmapWidth + (xPosition + x)]=backgroundPaletteIndex + + for char in text: - if backgroundPaletteIndex != 0: # the textbackground is different from the bitmap background - # draw a bounding box where the text will go + if char == '\n': # newline + xPosition=xStart # reset to left column + yPosition = yPosition + lineSpacingY(font, lineSpacing, scale) # Add a newline - (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 + else: - 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 ) - - # 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,y]] - - - - xPosition = xPosition + shift_x + 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) @@ -193,7 +223,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