151 lines
4.6 KiB
Python
151 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify the text rendering fixes for cropping and line length issues.
|
|
"""
|
|
|
|
from PIL import Image, ImageFont
|
|
from pyWebLayout.concrete.text import Text, Line
|
|
from pyWebLayout.style import Font, FontStyle, FontWeight
|
|
from pyWebLayout.style.layout import Alignment
|
|
import os
|
|
|
|
def test_text_cropping_fix():
|
|
"""Test that text is no longer cropped at the beginning and end"""
|
|
print("Testing text cropping fixes...")
|
|
|
|
# Create a font with a reasonable size
|
|
font_style = Font(
|
|
font_path=None, # Use default font
|
|
font_size=16,
|
|
colour=(0, 0, 0, 255),
|
|
weight=FontWeight.NORMAL,
|
|
style=FontStyle.NORMAL
|
|
)
|
|
|
|
# Test with text that might have overhang (like italic or characters with descenders)
|
|
test_texts = [
|
|
"Hello World!",
|
|
"Typography",
|
|
"gjpqy", # Characters with descenders
|
|
"AWVT", # Characters that might have overhang
|
|
"Italic Text"
|
|
]
|
|
|
|
for i, text_content in enumerate(test_texts):
|
|
print(f" Testing text: '{text_content}'")
|
|
text = Text(text_content, font_style)
|
|
|
|
# Verify dimensions are reasonable
|
|
print(f" Dimensions: {text.width}x{text.height}")
|
|
print(f" Text offsets: x={getattr(text, '_text_offset_x', 0)}, y={getattr(text, '_text_offset_y', 0)}")
|
|
|
|
# Render the text
|
|
rendered = text.render()
|
|
print(f" Rendered size: {rendered.size}")
|
|
|
|
# Save for visual inspection
|
|
output_path = f"test_text_{i}_{text_content.replace(' ', '_').replace('!', '')}.png"
|
|
rendered.save(output_path)
|
|
print(f" Saved as: {output_path}")
|
|
|
|
print("Text cropping test completed.\n")
|
|
|
|
def test_line_length_fix():
|
|
"""Test that lines are using the full available width properly"""
|
|
print("Testing line length fixes...")
|
|
|
|
font_style = Font(
|
|
font_path=None,
|
|
font_size=14,
|
|
colour=(0, 0, 0, 255)
|
|
)
|
|
|
|
# Create a line with specific width
|
|
line_width = 300
|
|
line_height = 20
|
|
spacing = (5, 10) # min, max spacing
|
|
|
|
line = Line(
|
|
spacing=spacing,
|
|
origin=(0, 0),
|
|
size=(line_width, line_height),
|
|
font=font_style,
|
|
halign=Alignment.LEFT
|
|
)
|
|
|
|
# Add words to the line
|
|
words = ["This", "is", "a", "test", "of", "line", "length", "calculation"]
|
|
|
|
print(f" Line width: {line_width}")
|
|
print(f" Adding words: {' '.join(words)}")
|
|
|
|
for word in words:
|
|
result = line.add_word(word)
|
|
if result:
|
|
print(f" Word '{word}' didn't fit, overflow: '{result}'")
|
|
break
|
|
else:
|
|
print(f" Added '{word}', current width: {line._current_width}")
|
|
|
|
print(f" Final line width used: {line._current_width}/{line_width}")
|
|
print(f" Words in line: {len(line.renderable_words)}")
|
|
|
|
# Render the line
|
|
rendered_line = line.render()
|
|
rendered_line.save("test_line_length.png")
|
|
print(f" Line saved as: test_line_length.png")
|
|
print(f" Rendered line size: {rendered_line.size}")
|
|
|
|
print("Line length test completed.\n")
|
|
|
|
def test_justification():
|
|
"""Test text justification to ensure proper spacing"""
|
|
print("Testing text justification...")
|
|
|
|
font_style = Font(
|
|
font_path=None,
|
|
font_size=12,
|
|
colour=(0, 0, 0, 255)
|
|
)
|
|
|
|
alignments = [
|
|
(Alignment.LEFT, "left"),
|
|
(Alignment.CENTER, "center"),
|
|
(Alignment.RIGHT, "right"),
|
|
(Alignment.JUSTIFY, "justify")
|
|
]
|
|
|
|
for alignment, name in alignments:
|
|
line = Line(
|
|
spacing=(3, 8),
|
|
origin=(0, 0),
|
|
size=(250, 18),
|
|
font=font_style,
|
|
halign=alignment
|
|
)
|
|
|
|
# Add some words
|
|
words = ["Testing", "text", "alignment", "and", "spacing"]
|
|
for word in words:
|
|
line.add_word(word)
|
|
|
|
rendered = line.render()
|
|
output_path = f"test_alignment_{name}.png"
|
|
rendered.save(output_path)
|
|
print(f" {name.capitalize()} alignment saved as: {output_path}")
|
|
|
|
print("Justification test completed.\n")
|
|
|
|
if __name__ == "__main__":
|
|
print("Running text rendering fix verification tests...\n")
|
|
|
|
test_text_cropping_fix()
|
|
test_line_length_fix()
|
|
test_justification()
|
|
|
|
print("All tests completed. Check the generated PNG files for visual verification.")
|
|
print("Look for:")
|
|
print("- Text should not be cropped at the beginning or end")
|
|
print("- Lines should use available width more efficiently")
|
|
print("- Different alignments should work correctly")
|