pyWebLayout/pyWebLayout/examples/demo_alignment_refactor.py
2025-06-08 17:17:09 +02:00

112 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""
Demonstration of the refactored alignment handler system.
This shows how the nested alignment logic has been replaced with a clean handler pattern.
"""
from pyWebLayout.concrete.text import (
Line, Text,
LeftAlignmentHandler, CenterRightAlignmentHandler, JustifyAlignmentHandler
)
from pyWebLayout.style.layout import Alignment
from pyWebLayout.style import Font
def demonstrate_handler_system():
"""Demonstrate the new alignment handler system."""
print("=" * 60)
print("ALIGNMENT HANDLER SYSTEM DEMONSTRATION")
print("=" * 60)
print("\n1. HANDLER CREATION:")
print(" The system now uses three specialized handlers:")
# Create handlers
left_handler = LeftAlignmentHandler()
center_handler = CenterRightAlignmentHandler(Alignment.CENTER)
right_handler = CenterRightAlignmentHandler(Alignment.RIGHT)
justify_handler = JustifyAlignmentHandler()
print(f" • LeftAlignmentHandler: {type(left_handler).__name__}")
print(f" • CenterRightAlignmentHandler (Center): {type(center_handler).__name__}")
print(f" • CenterRightAlignmentHandler (Right): {type(right_handler).__name__}")
print(f" • JustifyAlignmentHandler: {type(justify_handler).__name__}")
print("\n2. AUTOMATIC HANDLER SELECTION:")
print(" Lines automatically choose the correct handler based on alignment:")
font = Font()
line_size = (300, 30)
spacing = (5, 20)
alignments = [
(Alignment.LEFT, "Left"),
(Alignment.CENTER, "Center"),
(Alignment.RIGHT, "Right"),
(Alignment.JUSTIFY, "Justify")
]
for alignment, name in alignments:
line = Line(spacing, (0, 0), line_size, font, halign=alignment)
handler_type = type(line._alignment_handler).__name__
print(f"{name:7}{handler_type}")
print("\n3. HYPHENATION INTEGRATION:")
print(" Each handler has its own hyphenation strategy:")
# Sample text objects and test conditions
sample_text = [Text("Hello", font), Text("World", font)]
word_width = 80
available_width = 70 # Word doesn't fit
min_spacing = 5
handlers = [
("Left", left_handler),
("Center", center_handler),
("Right", right_handler),
("Justify", justify_handler)
]
for name, handler in handlers:
should_hyphenate = handler.should_try_hyphenation(
sample_text, word_width, available_width, min_spacing)
print(f"{name:7}: should_hyphenate = {should_hyphenate}")
print("\n4. SPACING CALCULATIONS:")
print(" Each handler calculates spacing and positioning differently:")
for name, handler in handlers:
spacing_calc, x_position = handler.calculate_spacing_and_position(
sample_text, 300, 5, 20)
print(f"{name:7}: spacing={spacing_calc:2d}, position={x_position:3d}")
print("\n5. WORD ADDITION WITH INTELLIGENT HYPHENATION:")
print(" The system now tries different hyphenation options for optimal spacing:")
# Test with a word that might benefit from hyphenation
test_line = Line(spacing, (0, 0), (200, 30), font, halign=Alignment.JUSTIFY)
test_words = ["This", "is", "a", "demonstration", "of", "smart", "hyphenation"]
for word in test_words:
result = test_line.add_word(word)
if result:
print(f" • Word '{word}' → remainder: '{result}' (line full)")
break
else:
print(f" • Added '{word}' successfully")
print(f" • Final line contains {len(test_line.text_objects)} text objects")
print("\n6. BENEFITS OF THE NEW SYSTEM:")
print(" ✓ Separation of concerns - each alignment has its own handler")
print(" ✓ Extensible - easy to add new alignment types")
print(" ✓ Intelligent hyphenation - considers spacing quality")
print(" ✓ Clean code - no more nested if/else alignment logic")
print(" ✓ Testable - each handler can be tested independently")
print("\n" + "=" * 60)
print("REFACTORING COMPLETE - ALIGNMENT HANDLERS WORKING!")
print("=" * 60)
if __name__ == "__main__":
demonstrate_handler_system()