@@ -43,13 +43,35 @@ class Text(Renderable, Queriable):
|
||||
# The bounding box is (left, top, right, bottom)
|
||||
try:
|
||||
bbox = font.getbbox(self._text)
|
||||
self._width = bbox[2] - bbox[0]
|
||||
self._height = bbox[3] - bbox[1]
|
||||
# Width is the difference between right and left
|
||||
self._width = max(1, bbox[2] - bbox[0])
|
||||
# Height needs to account for potential negative top values
|
||||
# Use the full height from top to bottom, ensuring positive values
|
||||
top = min(0, bbox[1]) # Account for negative ascenders
|
||||
bottom = max(bbox[3], bbox[1] + font.size) # Ensure minimum height
|
||||
self._height = max(font.size, bottom - top)
|
||||
self._size = (self._width, self._height)
|
||||
|
||||
# Store the offset for proper text positioning
|
||||
self._text_offset_x = max(0, -bbox[0])
|
||||
self._text_offset_y = max(0, -top)
|
||||
|
||||
except AttributeError:
|
||||
# Fallback for older PIL versions
|
||||
self._width, self._height = font.getsize(self._text)
|
||||
self._size = (self._width, self._height)
|
||||
try:
|
||||
self._width, self._height = font.getsize(self._text)
|
||||
# Add some padding to prevent cropping
|
||||
self._height = max(self._height, int(self._style.font_size * 1.2))
|
||||
self._size = (self._width, self._height)
|
||||
self._text_offset_x = 0
|
||||
self._text_offset_y = 0
|
||||
except:
|
||||
# Ultimate fallback
|
||||
self._width = len(self._text) * self._style.font_size // 2
|
||||
self._height = int(self._style.font_size * 1.2)
|
||||
self._size = (self._width, self._height)
|
||||
self._text_offset_x = 0
|
||||
self._text_offset_y = 0
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
@@ -123,8 +145,10 @@ class Text(Renderable, Queriable):
|
||||
if self._style.background and self._style.background[3] > 0: # If alpha > 0
|
||||
draw.rectangle([(0, 0), self._size], fill=self._style.background)
|
||||
|
||||
# Draw the text
|
||||
draw.text((0, 0), self._text, font=self._style.font, fill=self._style.colour)
|
||||
# Draw the text using calculated offsets to prevent cropping
|
||||
text_x = getattr(self, '_text_offset_x', 0)
|
||||
text_y = getattr(self, '_text_offset_y', 0)
|
||||
draw.text((text_x, text_y), self._text, font=self._style.font, fill=self._style.colour)
|
||||
|
||||
# Apply any text decorations
|
||||
self._apply_decoration(draw)
|
||||
|
||||
Reference in New Issue
Block a user