pyWebLayout/test_alignment_handlers.py
Duncan Tourolle 8d892bfe28
Some checks failed
Python CI / test (push) Failing after 4m28s
New alignement handlers
2025-06-08 14:08:29 +02:00

119 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify the alignment handler system is working correctly.
"""
from pyWebLayout.concrete.text import Line, Text, LeftAlignmentHandler, CenterRightAlignmentHandler, JustifyAlignmentHandler
from pyWebLayout.style.layout import Alignment
from pyWebLayout.style import Font
import numpy as np
def test_alignment_handlers():
"""Test all three alignment handlers."""
print("Testing Alignment Handler System")
print("=" * 40)
# Create a basic font for testing
font = Font()
# Test data
test_words = ["This", "is", "a", "test", "sentence"]
line_width = 300
line_height = 30
spacing = (5, 20) # min_spacing, max_spacing
# Test Left Alignment
print("\n1. Testing Left Alignment:")
left_line = Line(spacing, (0, 0), (line_width, line_height), font, halign=Alignment.LEFT)
for word in test_words:
result = left_line.add_word(word)
if result:
print(f" Word '{word}' didn't fit, remainder: '{result}'")
break
else:
print(f" Added word: '{word}'")
print(f" Final line has {len(left_line.text_objects)} words")
print(f" Handler type: {type(left_line._alignment_handler).__name__}")
# Test Center Alignment
print("\n2. Testing Center Alignment:")
center_line = Line(spacing, (0, 0), (line_width, line_height), font, halign=Alignment.CENTER)
for word in test_words:
result = center_line.add_word(word)
if result:
print(f" Word '{word}' didn't fit, remainder: '{result}'")
break
else:
print(f" Added word: '{word}'")
print(f" Final line has {len(center_line.text_objects)} words")
print(f" Handler type: {type(center_line._alignment_handler).__name__}")
# Test Right Alignment
print("\n3. Testing Right Alignment:")
right_line = Line(spacing, (0, 0), (line_width, line_height), font, halign=Alignment.RIGHT)
for word in test_words:
result = right_line.add_word(word)
if result:
print(f" Word '{word}' didn't fit, remainder: '{result}'")
break
else:
print(f" Added word: '{word}'")
print(f" Final line has {len(right_line.text_objects)} words")
print(f" Handler type: {type(right_line._alignment_handler).__name__}")
# Test Justify Alignment
print("\n4. Testing Justify Alignment:")
justify_line = Line(spacing, (0, 0), (line_width, line_height), font, halign=Alignment.JUSTIFY)
for word in test_words:
result = justify_line.add_word(word)
if result:
print(f" Word '{word}' didn't fit, remainder: '{result}'")
break
else:
print(f" Added word: '{word}'")
print(f" Final line has {len(justify_line.text_objects)} words")
print(f" Handler type: {type(justify_line._alignment_handler).__name__}")
# Test spacing and position calculations
print("\n5. Testing Handler Calculations:")
# Create sample text objects
text_objects = [Text(word, font) for word in ["Hello", "World"]]
handlers = [
("Left", LeftAlignmentHandler()),
("Center", CenterRightAlignmentHandler(Alignment.CENTER)),
("Right", CenterRightAlignmentHandler(Alignment.RIGHT)),
("Justify", JustifyAlignmentHandler())
]
for name, handler in handlers:
spacing_calc, position = handler.calculate_spacing_and_position(
text_objects, line_width, spacing[0], spacing[1])
print(f" {name}: spacing={spacing_calc}, position={position}")
print("\n6. Testing Hyphenation Decisions:")
# Test hyphenation decisions for different alignments
test_word_width = 50
available_width = 40 # Word doesn't fit
for name, handler in handlers:
should_hyphenate = handler.should_try_hyphenation(
text_objects, test_word_width, available_width, spacing[0])
print(f" {name}: should_hyphenate={should_hyphenate}")
print("\nAlignment Handler Test Completed Successfully!")
return True
if __name__ == "__main__":
test_alignment_handlers()