Skip to content

Commit

Permalink
New feature: Add limit_render_to_text_bbox to Label to limit text…
Browse files Browse the repository at this point in the history
… rendering to the text bounding box, improving `Label` alignments. (kivy#8510)

* add limit_render_to_text_bbox

* flake8
  • Loading branch information
DexerBR authored Dec 26, 2023
1 parent 5e5cfe4 commit 9576cf7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
15 changes: 14 additions & 1 deletion kivy/core/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ class LabelBase(object):
or `'weak_rtl'` (Pango only)
`text_language`: str, defaults to None (user locale)
RFC-3066 format language tag as a string (Pango only)
`limit_render_to_text_bbox`: bool, defaults to False. PIL only.
If set to ``True``, this parameter indicates that rendering should
be limited to the bounding box of the text, excluding any
additional white spaces designated for ascent and descent.
By limiting the rendering to the bounding box of the text, it
ensures a more precise alignment with surrounding elements when
utilizing properties such as `valign`, `y`, `pos`, `pos_hint`, etc.
.. versionadded:: 2.3.0
`limit_render_to_text_bbox` was added to allow to limit text rendering
to the text bounding box (PIL only).
.. deprecated:: 2.2.0
`padding_x` and `padding_y` have been deprecated. Please use `padding`
Expand Down Expand Up @@ -249,6 +260,7 @@ def __init__(
outline_width=None, outline_color=None, font_context=None,
font_features=None, base_direction=None, font_direction='ltr',
font_script_name='Latin', text_language=None,
limit_render_to_text_bbox=False,
**kwargs):

# Include system fonts_dir in resource paths.
Expand All @@ -273,7 +285,8 @@ def __init__(
'base_direction': base_direction,
'font_direction': font_direction,
'font_script_name': font_script_name,
'text_language': text_language}
'text_language': text_language,
'limit_render_to_text_bbox': limit_render_to_text_bbox}

kwargs_get = kwargs.get
options['color'] = color or (1, 1, 1, 1)
Expand Down
13 changes: 12 additions & 1 deletion kivy/core/text/text_pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def get_extents(self, text):
left, top, right, bottom = font.getbbox(text)
ascent, descent = font.getmetrics()

if self.options['limit_render_to_text_bbox']:
h = bottom - top
else:
h = ascent + descent
w = right - left
h = ascent + descent

return w, h

Expand All @@ -52,6 +55,14 @@ def _render_begin(self):

def _render_text(self, text, x, y):
color = tuple([int(c * 255) for c in self.options['color']])

# Adjust x and y position to avoid text cutoff
if self.options['limit_render_to_text_bbox']:
font = self._select_font()
bbox = font.getbbox(text)
x -= bbox[0]
y -= bbox[1]

self._pil_draw.text((int(x), int(y)),
text, font=self._select_font(), fill=color)

Expand Down
19 changes: 18 additions & 1 deletion kivy/uix/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ class Label(Widget):
'split_str', 'ellipsis_options', 'unicode_errors',
'markup', 'font_hinting', 'font_kerning',
'font_blended', 'font_context', 'font_features',
'base_direction', 'text_language')
'base_direction', 'text_language',
'limit_render_to_text_bbox')

def __init__(self, **kwargs):
self._trigger_texture = Clock.create_trigger(self.texture_update, -1)
Expand Down Expand Up @@ -1177,3 +1178,19 @@ def print_it(instance, value):
:attr:`font_blended` is a :class:`~kivy.properties.BooleanProperty` and
defaults to True.
'''

limit_render_to_text_bbox = BooleanProperty(False)
'''If set to ``True``, this parameter indicates that rendering should be
limited to the bounding box of the text, excluding any additional white
spaces designated for ascent and descent.
By limiting the rendering to the bounding box of the text, it ensures a
more precise alignment with surrounding elements when utilizing properties
such as `valign`, `y`, `pos`, `pos_hint`, etc.
.. note::
This feature requires the PIL text provider.
:attr:`limit_render_to_text_bbox` is a
:class:`~kivy.properties.BooleanProperty` and defaults to False.
'''

0 comments on commit 9576cf7

Please sign in to comment.