pyWebLayout/scripts/debug_text_positioning.py
Duncan Tourolle 993095caf9
Some checks failed
Python CI / test (push) Failing after 6m22s
Update repo
2025-11-04 19:25:44 +01:00

75 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""
Debug script to test text positioning in the line breaking system
"""
import sys
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
# Add pyWebLayout to path
sys.path.insert(0, str(Path(__file__).parent))
from pyWebLayout.style import Font
from pyWebLayout.concrete.text import Text, Line
from pyWebLayout.style.layout import Alignment
def test_simple_text_rendering():
"""Test basic text rendering to debug positioning issues"""
# Create a simple image
width, height = 300, 200
image = Image.new('RGB', (width, height), 'white')
draw = ImageDraw.Draw(image)
# Draw a border for reference
draw.rectangle([0, 0, width-1, height-1], outline=(200, 200, 200), width=2)
# Create a font
font = Font(font_size=12)
# Test 1: Direct PIL text rendering
print("Test 1: Direct PIL text rendering")
draw.text((10, 30), "Direct PIL text", font=font.font, fill=(0, 0, 0))
# Test 2: Using our Text class
print("Test 2: Using Text class")
text_obj = Text("Text class rendering", font, draw)
text_obj.set_origin([10, 60]) # Set position
print(f"Text origin: {text_obj.origin}")
text_obj.render()
# Test 3: Using Line class
print("Test 3: Using Line class")
line = Line(
spacing=(2, 6),
origin=(10, 100),
size=(280, 20),
draw=draw,
font=font,
halign=Alignment.LEFT
)
# Create a simple word to add to the line
from pyWebLayout.abstract.inline import Word
word = Word("Line class rendering", font)
success, overflow = line.add_word(word)
print(f"Word added successfully: {success}")
print(f"Line origin: {line.origin}")
print(f"Line baseline: {line._baseline}")
print(f"Text objects in line: {len(line.text_objects)}")
if line.text_objects:
for i, text in enumerate(line.text_objects):
print(f" Text {i}: '{text.text}' at origin {text.origin}")
line.render()
# Save the debug image
image.save("debug_text_positioning.png")
print("Debug image saved as debug_text_positioning.png")
if __name__ == "__main__":
test_simple_text_rendering()