Fix bugs with anchored_position and scaling
This commit is contained in:
parent
6862809305
commit
f37e66c2d3
2 changed files with 53 additions and 49 deletions
|
|
@ -88,6 +88,7 @@ class Label(displayio.Group):
|
|||
"""
|
||||
|
||||
# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals, too-many-arguments
|
||||
# pylint: disable=too-many-branches, no-self-use
|
||||
# Note: max_glyphs parameter is unnecessary, this is used for direct
|
||||
# compatibility with label.py
|
||||
|
||||
|
|
@ -231,16 +232,18 @@ class Label(displayio.Group):
|
|||
return_value = int(line_spacing * font.get_bounding_box()[1])
|
||||
return return_value
|
||||
|
||||
def _text_bounding_box(
|
||||
self, text, font, line_spacing, background_tight=False
|
||||
): # **** change default background_tight=False
|
||||
def _text_bounding_box(self, text, font, line_spacing, background_tight=False):
|
||||
|
||||
# This empirical approach checks several glyphs for maximum ascender and descender height
|
||||
# (consistent with label.py)
|
||||
glyphs = "M j'" # choose glyphs with highest ascender and lowest
|
||||
# descender, will depend upon font used
|
||||
|
||||
font.load_glyphs(text + glyphs)
|
||||
try:
|
||||
self._font.load_glyphs(text + glyphs)
|
||||
except AttributeError:
|
||||
# ignore if font does not have load_glyphs
|
||||
pass
|
||||
|
||||
ascender_max = descender_max = 0
|
||||
for char in glyphs:
|
||||
|
|
@ -251,8 +254,9 @@ class Label(displayio.Group):
|
|||
|
||||
lines = 1
|
||||
|
||||
xposition = x_start = 0 # starting x position (left margin)
|
||||
yposition = y_start = 0
|
||||
xposition = (
|
||||
x_start
|
||||
) = yposition = y_start = 0 # starting x and y position (left margin)
|
||||
|
||||
left = None
|
||||
right = x_start
|
||||
|
|
@ -297,24 +301,18 @@ class Label(displayio.Group):
|
|||
top = min(top, -my_glyph.height - my_glyph.dy + y_offset_tight)
|
||||
bottom = max(bottom, yposition - my_glyph.dy + y_offset_tight)
|
||||
|
||||
loose_height = (lines - 1) * self._line_spacing_ypixels(font, line_spacing) + (
|
||||
ascender_max + descender_max
|
||||
)
|
||||
|
||||
label_calibration_offset = int((font.get_glyph(ord("M")).height) / 2)
|
||||
|
||||
if left is None:
|
||||
left = 0
|
||||
|
||||
y_offset_tight = -top + label_calibration_offset
|
||||
|
||||
final_box_width = right - left
|
||||
if background_tight:
|
||||
final_box_height = bottom - top
|
||||
final_y_offset = y_offset_tight
|
||||
final_y_offset = -top + y_offset_tight
|
||||
|
||||
else:
|
||||
final_box_height = loose_height
|
||||
final_box_height = (lines - 1) * self._line_spacing_ypixels(
|
||||
font, line_spacing
|
||||
) + (ascender_max + descender_max)
|
||||
final_y_offset = ascender_max
|
||||
|
||||
return (final_box_width, final_box_height, left, final_y_offset)
|
||||
|
|
@ -518,16 +516,13 @@ class Label(displayio.Group):
|
|||
|
||||
# Set anchored_position
|
||||
if (self._anchor_point is not None) and (self._anchored_position is not None):
|
||||
new_x = int(
|
||||
self.x = int(
|
||||
new_position[0]
|
||||
- (self._bounding_box[0] * self._scale)
|
||||
- round(self._anchor_point[0] * (self._bounding_box[2] * self._scale))
|
||||
)
|
||||
new_y = int(
|
||||
round(
|
||||
new_position[1]
|
||||
- (self._anchor_point[1] * self._bounding_box[3] * self.scale)
|
||||
+ ((self._bounding_box[3] * self.scale) / 2.0)
|
||||
)
|
||||
self.y = int(
|
||||
new_position[1]
|
||||
- (self._bounding_box[1] * self._scale)
|
||||
- round(self._anchor_point[1] * self._bounding_box[3] * self._scale)
|
||||
)
|
||||
self.x = new_x
|
||||
self.y = new_y
|
||||
|
|
|
|||
|
|
@ -91,11 +91,6 @@ class Label(displayio.Group):
|
|||
self.x = x
|
||||
self.y = y
|
||||
|
||||
self.palette = displayio.Palette(2)
|
||||
self.palette[0] = 0
|
||||
self.palette.make_transparent(0)
|
||||
self.palette[1] = color
|
||||
|
||||
self.height = self._font.get_bounding_box()[1]
|
||||
self._line_spacing = line_spacing
|
||||
self._boundingbox = None
|
||||
|
|
@ -104,6 +99,12 @@ class Label(displayio.Group):
|
|||
background_tight # sets padding status for text background box
|
||||
)
|
||||
|
||||
# Create the two-color text palette
|
||||
self.palette = displayio.Palette(2)
|
||||
self.palette[0] = 0
|
||||
self.palette.make_transparent(0)
|
||||
self.color = color
|
||||
|
||||
self._background_color = background_color
|
||||
self._background_palette = displayio.Palette(1)
|
||||
self._added_background_tilegrid = False
|
||||
|
|
@ -177,8 +178,8 @@ class Label(displayio.Group):
|
|||
self._background_palette[0] = new_color
|
||||
self._background_color = new_color
|
||||
|
||||
lines = self._text.rstrip("\n").count("\n") + 1
|
||||
y_offset = int((self._font.get_glyph(ord("M")).height) / 2)
|
||||
lines = self.text.count("\n") + 1
|
||||
|
||||
if not self._added_background_tilegrid: # no bitmap is in the self Group
|
||||
# add bitmap if text is present and bitmap sizes > 0 pixels
|
||||
|
|
@ -223,16 +224,22 @@ class Label(displayio.Group):
|
|||
else:
|
||||
i = 0
|
||||
tilegrid_count = i
|
||||
|
||||
try:
|
||||
self._font.load_glyphs(new_text + "M")
|
||||
except AttributeError:
|
||||
# ignore if font does not have load_glyphs
|
||||
pass
|
||||
|
||||
y_offset = int((self._font.get_glyph(ord("M")).height) / 2)
|
||||
|
||||
right = top = bottom = 0
|
||||
left = None
|
||||
|
||||
lines = 1
|
||||
for character in new_text:
|
||||
if character == "\n":
|
||||
y += int(self.height * self._line_spacing)
|
||||
x = 0
|
||||
lines += 1
|
||||
continue
|
||||
glyph = self._font.get_glyph(ord(character))
|
||||
if not glyph:
|
||||
|
|
@ -312,7 +319,13 @@ class Label(displayio.Group):
|
|||
|
||||
@color.setter
|
||||
def color(self, new_color):
|
||||
self.palette[1] = new_color
|
||||
self._color = new_color
|
||||
if new_color is not None:
|
||||
self.palette[1] = new_color
|
||||
self.palette.make_opaque(1)
|
||||
else:
|
||||
self.palette[1] = 0
|
||||
self.palette.make_transparent(1)
|
||||
|
||||
@property
|
||||
def background_color(self):
|
||||
|
|
@ -377,14 +390,13 @@ class Label(displayio.Group):
|
|||
return (
|
||||
int(
|
||||
self.x
|
||||
+ round(self._anchor_point[0] * self._boundingbox[2] * self._scale)
|
||||
+ (self._boundingbox[0] * self._scale)
|
||||
+ +round(self._anchor_point[0] * self._boundingbox[2] * self._scale)
|
||||
),
|
||||
int(
|
||||
round(
|
||||
self.y
|
||||
+ (self._anchor_point[1] * self._boundingbox[3] * self._scale)
|
||||
- ((self._boundingbox[3] * self._scale) / 2.0)
|
||||
)
|
||||
self.y
|
||||
+ (self._boundingbox[1] * self._scale)
|
||||
+ round(self._anchor_point[1] * self._boundingbox[3] * self._scale)
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -392,16 +404,13 @@ class Label(displayio.Group):
|
|||
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(
|
||||
self.x = int(
|
||||
new_position[0]
|
||||
- (self._boundingbox[0] * self._scale)
|
||||
- round(self._anchor_point[0] * (self._boundingbox[2] * self._scale))
|
||||
)
|
||||
new_y = int(
|
||||
round(
|
||||
new_position[1]
|
||||
- (self._anchor_point[1] * self._boundingbox[3] * self._scale)
|
||||
+ ((self._boundingbox[3] * self._scale) / 2.0)
|
||||
)
|
||||
self.y = int(
|
||||
new_position[1]
|
||||
- (self._boundingbox[1] * self._scale)
|
||||
- round(self._anchor_point[1] * self._boundingbox[3] * self._scale)
|
||||
)
|
||||
self.x = new_x
|
||||
self.y = new_y
|
||||
|
|
|
|||
Loading…
Reference in a new issue