pyWebLayout/test_simple_pagination.py

146 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""
Simple test of pagination logic without EPUB dependencies
"""
from pyWebLayout.concrete.page import Page
from pyWebLayout.concrete.text import Text
from pyWebLayout.style.fonts import Font
from pyWebLayout.abstract.block import Paragraph
from pyWebLayout.abstract.inline import Word
def create_test_paragraph(text_content: str) -> Paragraph:
"""Create a test paragraph with the given text"""
paragraph = Paragraph()
words = text_content.split()
font = Font(font_size=16)
for word_text in words:
word = Word(word_text, font)
paragraph.add_word(word)
return paragraph
def test_simple_pagination():
"""Test pagination with simple content"""
print("=== Simple Pagination Test ===")
# Create test content - several paragraphs
test_paragraphs = [
"This is the first paragraph. It contains some text that should be rendered properly on the page. We want to see if this content appears correctly when we paginate.",
"Here is a second paragraph with different content. This paragraph should also appear on the page if there's enough space, or on the next page if the first paragraph fills it up.",
"The third paragraph continues with more text. This is testing whether our pagination logic works correctly and doesn't lose content.",
"Fourth paragraph here. We're adding more content to test how the pagination handles multiple blocks of text.",
"Fifth paragraph with even more content. This should help us see if the pagination is working as expected.",
"Sixth paragraph continues the pattern. We want to make sure no text gets lost during pagination.",
"Seventh paragraph adds more content. This is important for testing the fill-until-full logic.",
"Eighth paragraph here with more text to test pagination thoroughly."
]
# Convert to abstract blocks
blocks = []
for i, text in enumerate(test_paragraphs):
paragraph = create_test_paragraph(text)
blocks.append(paragraph)
print(f"Created paragraph {i+1}: {len(text.split())} words")
print(f"\nTotal blocks created: {len(blocks)}")
# Test page creation and filling
pages = []
current_page = Page(size=(700, 550))
print(f"\n=== Testing Block Addition ===")
for i, block in enumerate(blocks):
print(f"\nTesting block {i+1}...")
# Convert block to renderable
try:
renderable = current_page._convert_block_to_renderable(block)
if not renderable:
print(f" Block {i+1}: Could not convert to renderable")
continue
print(f" Block {i+1}: Converted to {type(renderable).__name__}")
# Store current state
children_backup = current_page._children.copy()
# Try adding to page
current_page.add_child(renderable)
# Try layout
try:
current_page.layout()
# Calculate height
max_bottom = 0
for child in current_page._children:
if hasattr(child, '_origin') and hasattr(child, '_size'):
child_bottom = child._origin[1] + child._size[1]
max_bottom = max(max_bottom, child_bottom)
print(f" Page height after adding: {max_bottom}")
# Check if page is too full
if max_bottom > 510: # Leave room for padding
print(f" Page full! Starting new page...")
# Rollback the last addition
current_page._children = children_backup
# Finalize current page
pages.append(current_page)
print(f" Finalized page {len(pages)} with {len(current_page._children)} children")
# Start new page
current_page = Page(size=(700, 550))
current_page.add_child(renderable)
current_page.layout()
# Calculate new page height
max_bottom = 0
for child in current_page._children:
if hasattr(child, '_origin') and hasattr(child, '_size'):
child_bottom = child._origin[1] + child._size[1]
max_bottom = max(max_bottom, child_bottom)
print(f" New page height: {max_bottom}")
else:
print(f" Block fits, continuing...")
except Exception as e:
print(f" Layout error: {e}")
current_page._children = children_backup
import traceback
traceback.print_exc()
except Exception as e:
print(f" Conversion error: {e}")
import traceback
traceback.print_exc()
# Add final page if it has content
if current_page._children:
pages.append(current_page)
print(f"\nFinalized final page {len(pages)} with {len(current_page._children)} children")
print(f"\n=== Pagination Results ===")
print(f"Total pages created: {len(pages)}")
for i, page in enumerate(pages):
print(f"Page {i+1}: {len(page._children)} blocks")
# Try to render each page
try:
rendered_image = page.render()
print(f" Rendered successfully: {rendered_image.size}")
except Exception as e:
print(f" Render error: {e}")
return pages
if __name__ == "__main__":
test_simple_pagination()