Skip to content

Add anchor_label method #223

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions adafruit_display_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"

from displayio import Group, Palette
from micropython import const

try:
from typing import List, Optional, Tuple
Expand Down Expand Up @@ -219,6 +220,17 @@ class LabelBase(Group):
:param bool verbose: print debugging information in some internal functions. Default to False
"""

# Anchor point constants for anchor_label method.
ANCHOR_TOP_LEFT = const((0.0, 0.0))
ANCHOR_TOP_CENTER = const((0.5, 0.0))
ANCHOR_TOP_RIGHT = const((1.0, 0.0))
ANCHOR_CENTER_LEFT = const((0.0, 0.5))
ANCHOR_CENTER = const((0.5, 0.5))
ANCHOR_CENTER_RIGHT = const((1.0, 0.5))
ANCHOR_BOTTOM_LEFT = const((0.0, 1.0))
ANCHOR_BOTTOM_CENTER = const((0.5, 1.0))
ANCHOR_BOTTOM_RIGHT = const((1.0, 1.0))

def __init__(
self,
font: FontProtocol,
Expand Down Expand Up @@ -456,3 +468,33 @@ def label_direction(self, new_label_direction: str) -> None:

def _replace_tabs(self, text: str) -> str:
return text if text.find("\t") < 0 else self._tab_text.join(text.split("\t"))

def anchor_label(self, anchor: Tuple[float, float], width: int, height: int) -> None:
"""Position the label on screen using the given anchor and display size.

:param anchor: One of the predefined anchor constants (e.g., ANCHOR_TOP_LEFT, ANCHOR_CENTER)
:param width: Width of the display in pixels
:param height: Height of the display in pixels
"""
if anchor not in {
LabelBase.ANCHOR_TOP_LEFT,
LabelBase.ANCHOR_TOP_CENTER,
LabelBase.ANCHOR_TOP_RIGHT,
LabelBase.ANCHOR_CENTER_LEFT,
LabelBase.ANCHOR_CENTER,
LabelBase.ANCHOR_CENTER_RIGHT,
LabelBase.ANCHOR_BOTTOM_LEFT,
LabelBase.ANCHOR_BOTTOM_CENTER,
LabelBase.ANCHOR_BOTTOM_RIGHT,
}:
raise ValueError(
"Anchor must be one of: ANCHOR_TOP_LEFT, ANCHOR_TOP_CENTER, ANCHOR_TOP_RIGHT,\n"
"ANCHOR_CENTER_LEFT, ANCHOR_CENTER, ANCHOR_CENTER_RIGHT,\n"
"ANCHOR_BOTTOM_LEFT, ANCHOR_BOTTOM_CENTER, ANCHOR_BOTTOM_RIGHT."
)

self.anchor_point = anchor
self.anchored_position = (
int(anchor[0] * width),
int(anchor[1] * height),
)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy