community icon indicating copy to clipboard operation
community copied to clipboard

Vertically centered text drops a wrapped line even when it fits

Open darshpatel1052 opened this issue 3 weeks ago • 0 comments

Software Versions

  • Python: 3.14.0
  • OS: Linux x86_64, kernel 7.0.0-42-generic, glibc 2.39
  • Kivy: 3.0.0.dev0, master at f316ec0ab181155a7306c0ab10157011be3d705e
  • Kivy installation method: local source checkout using the PIL text provider, Pillow 11.3.0, and bundled Roboto. The master checkout reused the existing local build's native extensions; the native sources and build definitions were unchanged between the two revisions tested.

Describe the bug Vertically centered text can lose a wrapped line even when the complete text fits inside text_size.

Using a short title followed by a wrapped paragraph, the text needs 120 px at a width of 400 px. With text_size=(400, 150), valign="middle" and valign="center" retain only four of the five lines. The last line, several lines , is missing. Top and bottom alignment retain all five lines with the same dimensions.

I reproduced this on current master and on the #9493 branch at 88074b5fed84379bf3bad5a00393abac0648d435. Increasing the box height to 240 px retains all five lines for both centered aliases.

Expected behavior Centering should retain all five lines when the complete text fits inside the available height.

To Reproduce Save the script below as reproduce_clipping.py and run it in an environment with Kivy master and Pillow installed:

KIVY_NO_ARGS=1 KIVY_TEXT=pil python reproduce_clipping.py

The script calls the real core text Label.render() with actual font metrics. It reads the resulting layout without creating a Window or texture. The final assertion deliberately reports missing lines; it is a diagnostic assertion, not an exception raised by Kivy itself.

Code and Logs and screenshots

"""Report line loss in vertically centered text, without a Window or texture.

The text sample comes from ElliotGarbus's remaining example in kivy/kivy#9477.
The final assertion fails when fitting text is clipped.
"""

import os
import platform
import sys
from importlib.metadata import version

os.environ.setdefault("KIVY_NO_ARGS", "1")
os.environ.setdefault("KIVY_TEXT", "pil")

import kivy  # noqa: E402
from kivy.core.text import Label  # noqa: E402


TEXT = "Title\n" + "words that wrap onto several lines " * 4
WIDTH = 400
HEIGHT = 150


def layout(height, valign):
    label = Label(
        text=TEXT, text_size=(WIDTH, height), font_name="Roboto",
        font_size=20, halign="left", valign=valign,
    )
    label.render()
    lines = ["".join(word.text for word in line.words)
             for line in label._cached_lines]
    return label._internal_size[1], lines


print("Python:", sys.version.split()[0])
print("OS:", platform.platform())
print("Kivy:", kivy.__version__)
print("Kivy source:", kivy.__file__)
print("Text provider:", Label.__module__ + "." + Label.__name__)
print("Pillow:", version("Pillow"))

needed, expected = layout(None, "top")
print(f"Unconstrained: height={needed}, lines={len(expected)}")
print("Expected lines:", expected)
assert needed <= HEIGHT, "This font/environment does not fit the sample."

failures = []
for height in (HEIGHT, max(HEIGHT, int(needed * 2))):
    for valign in ("top", "bottom", "middle", "center"):
        used, actual = layout(height, valign)
        print(f"box={height}, valign={valign}: "
              f"height={used}, lines={len(actual)}/{len(expected)}")
        print("Retained lines:", actual)
        if actual != expected:
            failures.append((height, valign, len(actual)))

assert not failures, f"Fitting text was clipped: {failures}"

Both revisions produced these results:

Box height Alignment Retained lines
Unconstrained top 5, needing 120 px
150 px top / bottom 5 of 5
150 px middle / center 4 of 5
240 px top / bottom / middle / center 5 of 5

The script ended with:

AssertionError: Fitting text was clipped: [(150, 'middle', 4), (150, 'center', 4)]

Complete output from both runs and the runnable script: https://gist.github.com/darshpatel1052/7e99b53c2fc117510fa037b18777c42b

Additional context This follows up on ElliotGarbus's example in #9477 and the request on #9493. The text sample is from ElliotGarbus's example.

The centered layout splits at an explicit newline chosen around the character midpoint, then gives the text below that split half the available height. Here, the only newline is after Title, so the wrapped paragraph is limited to 75 px in a 150 px box. The complete text needs only 120 px, but that first layout step has already dropped a line. The newline distance correction in #9493 does not change this example because there is only one explicit newline.

Verification was headless with the PIL provider. I have not checked a GUI screenshot or other text providers for this report. No fix is included.

darshpatel1052 avatar Nov 11 '26 16:11 darshpatel1052