@@ -12,16 +12,26 @@ A Python library for HTML-like layout and rendering.
|
||||
> 📋 **Note**: Badges show results from the commit referenced in the URLs. Red "error" badges indicate build failures for that specific step.
|
||||
## Description
|
||||
|
||||
PyWebLayout provides classes for rendering HTML-like content to images using a box-based layout system. It includes support for text, tables, and containers, as well as an HTML parser for converting HTML to layout objects.
|
||||
PyWebLayout is a Python library for rendering HTML and EPUB content to paginated images. The library provides a high-level **EbookReader** API for building interactive ebook reader applications, along with powerful HTML-to-page rendering capabilities.
|
||||
|
||||
## Features
|
||||
## Key Features
|
||||
|
||||
- HTML-like layout system
|
||||
- Text rendering with font support
|
||||
- Table layouts
|
||||
- Container elements
|
||||
- HTML parsing
|
||||
- Image output
|
||||
### EbookReader - High-Level API
|
||||
- 📖 **EPUB Support** - Load and render EPUB files
|
||||
- 📄 **Page Rendering** - Render pages as PIL Images
|
||||
- ⬅️➡️ **Navigation** - Forward and backward page navigation
|
||||
- 🔖 **Bookmarks** - Save and load reading positions
|
||||
- 📑 **Chapter Navigation** - Jump to chapters by title or index
|
||||
- 🔤 **Font Control** - Adjust font size dynamically
|
||||
- 📏 **Spacing Control** - Customize line and paragraph spacing
|
||||
- 📊 **Progress Tracking** - Monitor reading progress
|
||||
|
||||
### Core Capabilities
|
||||
- HTML-to-page layout system
|
||||
- Multi-page document rendering
|
||||
- Advanced text rendering with font support
|
||||
- Position tracking across layout changes
|
||||
- Intelligent line breaking and pagination
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -29,69 +39,77 @@ PyWebLayout provides classes for rendering HTML-like content to images using a b
|
||||
pip install pyWebLayout
|
||||
```
|
||||
|
||||
## Usage
|
||||
## Quick Start
|
||||
|
||||
### Basic Example
|
||||
### EbookReader - Recommended API
|
||||
|
||||
```python
|
||||
from pyWebLayout.concrete.page import Page, Container
|
||||
from pyWebLayout.abstract.inline import Line
|
||||
from pyWebLayout.layout import Alignment
|
||||
from PIL import ImageFont
|
||||
from pyWebLayout.layout.ereader_application import EbookReader
|
||||
|
||||
# Create a page
|
||||
page = Page(size=(800, 600), background_color=(240, 240, 240))
|
||||
|
||||
# Add a title container
|
||||
title_container = Container(
|
||||
origin=(0, 0),
|
||||
size=(780, 60),
|
||||
direction='horizontal',
|
||||
spacing=10,
|
||||
padding=(10, 10, 10, 10),
|
||||
halign=Alignment.CENTER,
|
||||
valign=Alignment.CENTER
|
||||
)
|
||||
page.add_child(title_container)
|
||||
|
||||
# Create a title line with text
|
||||
title_font = ImageFont.load_default()
|
||||
title_line = Line(
|
||||
spacing=(8, 15),
|
||||
origin=(0, 0),
|
||||
size=(760, 40),
|
||||
font=title_font,
|
||||
text_color=(0, 0, 0),
|
||||
halign=Alignment.CENTER
|
||||
)
|
||||
title_container.add_child(title_line)
|
||||
title_line.add_word("PyWebLayout", title_font)
|
||||
title_line.add_word("Example", title_font)
|
||||
|
||||
# Layout and render the page
|
||||
page.layout()
|
||||
image = page.render()
|
||||
image.save("example.png")
|
||||
# Create an ebook reader
|
||||
with EbookReader(page_size=(800, 1000)) as reader:
|
||||
# Load an EPUB file
|
||||
reader.load_epub("mybook.epub")
|
||||
|
||||
# Get current page as PIL Image
|
||||
page = reader.get_current_page()
|
||||
page.save("page_001.png")
|
||||
|
||||
# Navigate through pages
|
||||
reader.next_page()
|
||||
reader.previous_page()
|
||||
|
||||
# Save reading position
|
||||
reader.save_position("chapter_3")
|
||||
|
||||
# Jump to a chapter
|
||||
reader.jump_to_chapter("Chapter 5")
|
||||
|
||||
# Adjust font size
|
||||
reader.increase_font_size()
|
||||
|
||||
# Get progress
|
||||
progress = reader.get_reading_progress()
|
||||
print(f"Progress: {progress*100:.1f}%")
|
||||
```
|
||||
|
||||
### HTML Example
|
||||
### HTML Multi-Page Rendering
|
||||
|
||||
```python
|
||||
from pyWebLayout.html_parser import html_to_image
|
||||
from pyWebLayout.io.readers.html_extraction import html_to_blocks
|
||||
from pyWebLayout.layout.document_layouter import paragraph_layouter
|
||||
from pyWebLayout.concrete.page import Page
|
||||
|
||||
# Parse HTML to blocks
|
||||
html = """
|
||||
<div style="text-align: center; padding: 10px;">
|
||||
<h1>PyWebLayout HTML Example</h1>
|
||||
<p>This is a paragraph rendered from HTML.</p>
|
||||
<p>The library supports <b>bold</b>, <i>italic</i>, and <u>underlined</u> text.</p>
|
||||
</div>
|
||||
<h1>Document Title</h1>
|
||||
<p>First paragraph with <b>bold</b> text.</p>
|
||||
<p>Second paragraph with more content.</p>
|
||||
"""
|
||||
blocks = html_to_blocks(html)
|
||||
|
||||
# Render HTML to an image
|
||||
image = html_to_image(html, page_size=(800, 600))
|
||||
image.save("html_example.png")
|
||||
# Render to pages
|
||||
page = Page(size=(600, 800))
|
||||
# Layout blocks onto pages using document_layouter
|
||||
# See examples/ directory for complete multi-page examples
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
Check out the `examples/` directory for complete working examples:
|
||||
|
||||
- **`simple_ereader_example.py`** - Quick start with EbookReader
|
||||
- **`ereader_demo.py`** - Comprehensive EbookReader feature demo
|
||||
- **`html_multipage_demo.py`** - HTML to multi-page rendering
|
||||
- See `examples/README.md` for full list
|
||||
|
||||
## Documentation
|
||||
|
||||
- **EbookReader API**: `examples/README_EREADER.md`
|
||||
- **HTML Rendering**: `examples/README_HTML_MULTIPAGE.md`
|
||||
- **Architecture**: `ARCHITECTURE.md`
|
||||
- **Examples**: `examples/README.md`
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
|
||||
Reference in New Issue
Block a user