#!/usr/bin/env python3 """ Table Text Wrapping Example This example demonstrates the line wrapping functionality in table cells: - Tables with long text that wraps across multiple lines - Automatic word wrapping within cell boundaries - Hyphenation support for long words - Multiple paragraphs per cell - Comparison of narrow vs. wide columns Shows how the Line-based text layout system handles text overflow in tables. """ from pyWebLayout.io.readers.html_extraction import parse_html_string from pyWebLayout.layout.document_layouter import DocumentLayouter from pyWebLayout.style.page_style import PageStyle from pyWebLayout.concrete.table import TableStyle from pyWebLayout.concrete.page import Page import sys from pathlib import Path from PIL import Image # Add pyWebLayout to path sys.path.insert(0, str(Path(__file__).parent.parent)) def create_narrow_columns_example(): """Create a table with narrow columns to show aggressive wrapping.""" print(" - Narrow columns with text wrapping") html = """
| Feature | Description | Benefits |
|---|---|---|
| Automatic Line Wrapping | Text automatically wraps to fit within the available cell width, creating multiple lines as needed. | Improves readability and prevents horizontal overflow in tables. |
| Hyphenation Support | Long words are intelligently hyphenated using pyphen library or brute-force splitting when necessary. | Handles extraordinarily long words that wouldn't fit on a single line. |
| Multi-paragraph Cells | Each cell can contain multiple paragraphs or headings, all properly wrapped. | Allows rich content within table cells. |
| Product | Short Description | Detailed Features |
|---|---|---|
| Widget Pro | Premium | Advanced functionality with enterprise-grade reliability, comprehensive warranty coverage, and dedicated customer support available around the clock. |
| Widget Lite | Basic | Essential features for everyday use with straightforward operation and minimal learning curve. |
| Widget Max | Ultimate | Everything from Widget Pro plus additional customization options, API integration capabilities, and advanced analytics dashboard. |
| API Method | Parameters | Description | Return Value |
|---|---|---|---|
| render_table() | table, origin, width, draw, style | Renders a table with automatic text wrapping in cells. Uses the Line class for intelligent word placement and hyphenation. | Rendered table with calculated height and width properties. |
| add_word() | word, pretext | Attempts to add a word to the current line. If it doesn't fit, tries hyphenation strategies including pyphen and brute-force splitting. | Tuple of (success, overflow_text) indicating whether word was added and any remaining text. |
| calculate_spacing() | text_objects, width, min_spacing, max_spacing | Determines optimal spacing between words to achieve proper justification within the specified constraints. | Calculated spacing value and position offset for alignment. |
| Date | Headline | Summary |
|---|---|---|
| 2024-01-15 | New Text Wrapping Feature | PyWebLayout now supports automatic line wrapping in table cells, bringing sophisticated text layout capabilities to table rendering. The implementation leverages the existing Line class infrastructure. |
| 2024-01-10 | Hyphenation Improvements | Enhanced hyphenation algorithms now include both dictionary-based pyphen hyphenation and intelligent brute-force splitting for edge cases. |
| 2024-01-05 | Performance Optimization | Table rendering performance improved through better caching and reduced font object creation overhead. |