Skip to content

Fix the position of bitmap_label with padding and anchored_position #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Neradoc
Copy link
Contributor

@Neradoc Neradoc commented Mar 10, 2023

When adding padding to a bitmap label and setting its anchored_position, the location of the label is incorrect.
This fixes the calculation, and makes bitmap labels display the same way as labels when possible.
Note: I assumed that the intention is for the position to be based on the corner of the text, and not the full box.

The first test shows each label.Label in grey (correctly placed), and the bitmap_label.Label in color on top of it.
The other test shows the fixed bitmap_label.Label in green on top of the old one in red.

Before
bitmap_label-before-pr

After
bitmap_label-after-pr

Here are DWR and UPR bitmap_label, fixed in green on top of the current ones in red. (No comparison with label.Label because or #185 which should be fixed on its own).

paddings-bitmap-label-fixed-unfixed

# setup the display (this was tested on a CPB with TFT gizmo)
# display = ...
from adafruit_gizmo import tft_gizmo
display = tft_gizmo.TFT_Gizmo()

from adafruit_display_text_old import label as label0, bitmap_label as bitmap_label0
from adafruit_display_text import label, bitmap_label

import time
import displayio
import terminalio
import vectorio

# Make the display context
splash = displayio.Group(x=1, y=1, scale=2)
display.show(splash)
display.auto_refresh = False

color_palette = displayio.Palette(2)
color_palette[0] = 0x008000  # Bright Green
color_palette[1] = 0x808080 # 0xAA0088

lines = displayio.Group()
for num in range(21):
    y = 10 * num
    color = min(1, num % 5)
    lines.append(vectorio.Rectangle(pixel_shader=color_palette,
        width=display.width, height=1, x=0, y=y, color_index=color))
    lines.append(vectorio.Rectangle(pixel_shader=color_palette,
        width=1, height=display.height, x=y, y=0, color_index=color))

text_group = displayio.Group(scale=1, x=0, y=0)
def add_text(group=text_group, back_class=label, text="AB",
             border=0, x=0, y=0, anchor=(0,0), scale=1,
             direction="LTR", color=0xFF0000, back_color=0x606060
        ):
    position = (x, y)
    if isinstance(border, int):
        border = (border, border, border, border)
    if back_class:
        text_area2 = back_class.Label(
            terminalio.FONT, text=text, color=0xFFFFFF,
            anchor_point=anchor,
            anchored_position=position,
            padding_top = border[0],
            padding_bottom = border[1],
            padding_left = border[2],
            padding_right = border[3],
            background_color = back_color,
            scale = scale,
            label_direction=direction,
        )
        group.append(text_area2)
    text_area1 = bitmap_label.Label(
        terminalio.FONT, text=text, color=0xFFFFFF,
        anchor_point=anchor,
        anchored_position=position,
        padding_top = border[0],
        padding_bottom = border[1],
        padding_left = border[2],
        padding_right = border[3],
        background_color = color,
        scale = scale,
        label_direction=direction,
    )
    group.append(text_area1)

########################################################################
# directions: LTR / RTL / UPR / DWR

position_group = displayio.Group()
pp = {"group":position_group}
add_text(border=5, x=10, y=10, anchor=(0,0), **pp)
add_text(border=5, x=10, y=40, anchor=(0,0), direction="DWR", color=0x0000FF, **pp)
add_text(border=5, x=80, y=20, anchor=(1,1), direction="RTL", color=0x008000, **pp)
add_text(border=5, x=80, y=50, anchor=(1,1), direction="UPR", color=0xA08000, **pp)
add_text(border=0, x=40, y=10, anchor=(0,0), color=0xFF8080, **pp)
add_text(border=0, x=50, y=50, anchor=(1,1), color=0xFF8080, **pp)
text_group.append(position_group)

########################################################################

anchor = (0,0) # (1,1)
offset = 0     # 20

########################################################################

padding_group = displayio.Group(y=60 + offset)
parameters = {
    "group":padding_group,
    "direction":"DWR",
    "color":0x008000,
    "back_class":bitmap_label0,
    "back_color":0xFF0000,
    "anchor":anchor,
}
add_text(border=(5,0,0,0), x=10, y=0, text="TOP", **parameters)
add_text(border=(0,5,0,0), x=40, y=0, text="BTM", **parameters)
add_text(border=(0,0,5,0), x=70, y=0, text="LFT", **parameters)
add_text(border=(0,0,0,5), x=100, y=0, text="RGT", **parameters)
text_group.append(padding_group)

########################################################################

padding_group2 = displayio.Group(y=90 + offset)
parameters = {
    "group":padding_group2,
    "direction":"UPR",
    "color":0x008000,
    "back_class":bitmap_label0,
    "back_color":0xFF0000,
    "anchor":anchor,
}
add_text(border=(5,0,0,0), x=10, y=0, text="TOP", **parameters)
add_text(border=(0,5,0,0), x=40, y=0, text="BTM", **parameters)
add_text(border=(0,0,5,0), x=70, y=0, text="LFT", **parameters)
add_text(border=(0,0,0,5), x=100, y=0, text="RGT", **parameters)
text_group.append(padding_group2)

########################################################################

splash.append(text_group)
splash.append(lines)
display.refresh()

while True:
    pass

Copy link
Contributor

@jposada202020 jposada202020 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!!! This looks good to me. Was tested on Adafruit CircuitPython 8.1.0-beta.0-19-gb6a761335-dirty on 2023-03-10; Adafruit PyPortal Titano with samd51j20 .
This solved the localization of the label as seen in picture of #185.
Was tested with both the script provided in the PR and the one provided in issue #185. Both work accordingly.

This is great. Thank you very much

@jposada202020 jposada202020 merged commit 966c3ad into adafruit:main Mar 11, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 13, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_BNO08X to 1.1.10 from 1.1.9:
  > Merge pull request adafruit/Adafruit_CircuitPython_BNO08x#39 from xenomorpheus/main
  > Add upload url to release action
  > Add .venv to .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_DRV2605 to 1.3.0 from 1.2.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_DRV2605#34 from kriswilk/patch-1
  > Add upload url to release action
  > Add .venv to .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_LC709203F to 2.2.11 from 2.2.10:
  > Merge pull request adafruit/Adafruit_CircuitPython_LC709203F#23 from edanidzerda/main
  > Add upload url to release action

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE to 9.0.2 from 9.0.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE#183 from brentyi/main
  > Add upload url to release action
  > Add .venv to .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Text to 2.26.0 from 2.24.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#184 from Neradoc/fix-multiple-lines-in-bitmaplabel
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#186 from Neradoc/fix-bitmap-label-anchored-position

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_GFX

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants