diff --git a/docs/images/example_04_table_rendering.png b/docs/images/example_04_table_rendering.png index b0f5879..d363771 100644 Binary files a/docs/images/example_04_table_rendering.png and b/docs/images/example_04_table_rendering.png differ diff --git a/docs/images/example_11_table_text_wrapping.png b/docs/images/example_11_table_text_wrapping.png new file mode 100644 index 0000000..2706324 Binary files /dev/null and b/docs/images/example_11_table_text_wrapping.png differ diff --git a/docs/images/example_11b_simple_wrapping.png b/docs/images/example_11b_simple_wrapping.png new file mode 100644 index 0000000..39467d5 Binary files /dev/null and b/docs/images/example_11b_simple_wrapping.png differ diff --git a/examples/11_table_text_wrapping_demo.py b/examples/11_table_text_wrapping_demo.py new file mode 100644 index 0000000..0998f20 --- /dev/null +++ b/examples/11_table_text_wrapping_demo.py @@ -0,0 +1,312 @@ +#!/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. | +
| Column 1 | +Column 2 | +Column 3 | +
|---|---|---|
| This is a cell with quite a lot of text that will need to wrap across multiple lines. | +Short text | +Another cell with enough content to demonstrate the automatic line wrapping functionality. | +
| Cell A | +This middle cell contains a paragraph with several words that should wrap nicely within the available space. | +Cell C | +
| Words like supercalifragilisticexpialidocious might need hyphenation. | +Normal text | +The wrapping algorithm handles both regular word wrapping and hyphenation seamlessly. | +