163 lines
5.7 KiB
Python
163 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script specifically for verifying the long word fix.
|
|
"""
|
|
|
|
from PIL import Image, ImageDraw
|
|
from pyWebLayout.concrete.text import Text, Line
|
|
from pyWebLayout.style import Font, FontStyle, FontWeight
|
|
from pyWebLayout.style.layout import Alignment
|
|
|
|
def test_supercalifragilisticexpialidocious():
|
|
"""Test the specific long word that was causing issues"""
|
|
|
|
print("Testing long word handling...")
|
|
|
|
font_style = Font(
|
|
font_path=None,
|
|
font_size=12,
|
|
colour=(0, 0, 0, 255)
|
|
)
|
|
|
|
# The problematic sentence
|
|
sentence = "This sentence has some really long words like supercalifragilisticexpialidocious that might need hyphenation."
|
|
|
|
# Test with the same constraints that were failing
|
|
line_width = 150
|
|
line_height = 25
|
|
|
|
words = sentence.split()
|
|
|
|
# Create lines and track all the text
|
|
lines = []
|
|
words_remaining = words.copy()
|
|
all_rendered_text = []
|
|
|
|
print(f"Original sentence: {sentence}")
|
|
print(f"Line width: {line_width}px")
|
|
print()
|
|
|
|
line_number = 1
|
|
while words_remaining:
|
|
print(f"Creating line {line_number}...")
|
|
|
|
# Create a new line
|
|
current_line = Line(
|
|
spacing=(3, 8),
|
|
origin=(0, (line_number-1) * line_height),
|
|
size=(line_width, line_height),
|
|
font=font_style,
|
|
halign=Alignment.LEFT
|
|
)
|
|
|
|
lines.append(current_line)
|
|
|
|
# Add words to current line until it's full
|
|
words_added_to_line = []
|
|
while words_remaining:
|
|
word = words_remaining[0]
|
|
print(f" Trying to add word: '{word}'")
|
|
|
|
result = current_line.add_word(word)
|
|
|
|
if result is None:
|
|
# Word fit in the line
|
|
words_added_to_line.append(word)
|
|
words_remaining.pop(0)
|
|
print(f" ✓ Added '{word}' to line {line_number}")
|
|
else:
|
|
# Word didn't fit, or only part of it fit
|
|
if result == word:
|
|
# Whole word didn't fit
|
|
print(f" ✗ Word '{word}' didn't fit, moving to next line")
|
|
break
|
|
else:
|
|
# Part of word fit, remainder is in result
|
|
words_added_to_line.append(word) # The original word
|
|
words_remaining[0] = result # Replace with remainder
|
|
print(f" ⚡ Part of '{word}' fit, remainder: '{result}'")
|
|
break
|
|
|
|
# Show what's on this line
|
|
line_words = [word.text for word in current_line.text_objects]
|
|
line_text = ' '.join(line_words)
|
|
all_rendered_text.extend(line_words)
|
|
print(f" Line {line_number} contains: \"{line_text}\"")
|
|
print(f" Line {line_number} width usage: {current_line._current_width}/{line_width}px")
|
|
print()
|
|
|
|
# If no words were added to this line, we have a problem
|
|
if not line_words:
|
|
print(f"ERROR: No words could be added to line {line_number}")
|
|
break
|
|
|
|
line_number += 1
|
|
|
|
# Safety check to prevent infinite loops
|
|
if line_number > 10:
|
|
print("Safety break: too many lines")
|
|
break
|
|
|
|
# Check if all words were rendered
|
|
original_words = sentence.split()
|
|
rendered_text_combined = ' '.join(all_rendered_text)
|
|
|
|
print("="*60)
|
|
print("VERIFICATION")
|
|
print("="*60)
|
|
print(f"Original text: {sentence}")
|
|
print(f"Rendered text: {rendered_text_combined}")
|
|
print()
|
|
|
|
# Check for the problematic word
|
|
long_word = "supercalifragilisticexpialidocious"
|
|
if long_word in rendered_text_combined:
|
|
print(f"✓ SUCCESS: Long word '{long_word}' was rendered!")
|
|
elif "supercalifragilisticexpialidocious" in rendered_text_combined:
|
|
print(f"✓ SUCCESS: Long word was rendered (possibly hyphenated)!")
|
|
else:
|
|
# Check if parts of the word are there
|
|
found_parts = []
|
|
for rendered_word in all_rendered_text:
|
|
if long_word.startswith(rendered_word.replace('-', '')):
|
|
found_parts.append(rendered_word)
|
|
elif rendered_word.replace('-', '') in long_word:
|
|
found_parts.append(rendered_word)
|
|
|
|
if found_parts:
|
|
print(f"✓ PARTIAL SUCCESS: Found parts of long word: {found_parts}")
|
|
else:
|
|
print(f"✗ FAILURE: Long word '{long_word}' was not rendered at all!")
|
|
|
|
print(f"Total lines used: {len(lines)}")
|
|
|
|
# Create combined image showing all lines
|
|
total_height = len(lines) * line_height
|
|
combined_image = Image.new('RGBA', (line_width, total_height), (255, 255, 255, 255))
|
|
|
|
for i, line in enumerate(lines):
|
|
line_img = line.render()
|
|
y_pos = i * line_height
|
|
combined_image.paste(line_img, (0, y_pos), line_img)
|
|
|
|
# Add a border for visualization
|
|
draw = ImageDraw.Draw(combined_image)
|
|
draw.rectangle([(0, y_pos), (line_width-1, y_pos + line_height-1)], outline=(200, 200, 200), width=1)
|
|
|
|
# Save the result
|
|
output_filename = "test_long_word_fix.png"
|
|
combined_image.save(output_filename)
|
|
print(f"Result saved as: {output_filename}")
|
|
|
|
return len(lines), all_rendered_text
|
|
|
|
if __name__ == "__main__":
|
|
print("Testing long word fix for 'supercalifragilisticexpialidocious'...\n")
|
|
|
|
lines_used, rendered_words = test_supercalifragilisticexpialidocious()
|
|
|
|
print(f"\nTest completed!")
|
|
print(f"- Lines used: {lines_used}")
|
|
print(f"- Total words rendered: {len(rendered_words)}")
|
|
print(f"- Check test_long_word_fix.png for visual verification")
|