Compare commits
No commits in common. "11dc30ba8de7cd0b7c05de2a34ec7051f9a370b9" and "1993b0bf48e62b678e0bd966bcd478e90b1732a8" have entirely different histories.
11dc30ba8d
...
1993b0bf48
Binary file not shown.
|
Before Width: | Height: | Size: 1.9 MiB After Width: | Height: | Size: 1.5 MiB |
@ -361,49 +361,6 @@ def main():
|
|||||||
else:
|
else:
|
||||||
print(" Skipping - button not found")
|
print(" Skipping - button not found")
|
||||||
|
|
||||||
# Get current overlay state for word spacing section
|
|
||||||
current_overlay3 = reader.get_current_page()
|
|
||||||
|
|
||||||
# Frame W: Tap on word spacing increase button
|
|
||||||
print("Frame W: Tap on word spacing increase...")
|
|
||||||
if 'setting:word_spacing_increase' in link_positions:
|
|
||||||
tap_x4, tap_y4 = link_positions['setting:word_spacing_increase']
|
|
||||||
print(f" Using coordinates: ({tap_x4}, {tap_y4})")
|
|
||||||
|
|
||||||
tap_visual4 = add_tap_indicator(current_overlay3, tap_x4, tap_y4, "Increase")
|
|
||||||
annotated_ws_tap = add_gesture_annotation(tap_visual4, "Tap to increase word spacing", "bottom")
|
|
||||||
frames.append(annotated_ws_tap)
|
|
||||||
frame_duration.append(1500) # 1.5 seconds
|
|
||||||
|
|
||||||
# Frames W+1 to W+5: Word spacing increased (live preview) - show each tap individually
|
|
||||||
print("Frames W+1 to W+5: Word spacing increased with live preview (5 taps, showing each)...")
|
|
||||||
for i in range(5):
|
|
||||||
event_tap_word = TouchEvent(gesture=GestureType.TAP, x=tap_x4, y=tap_y4)
|
|
||||||
response = reader.handle_touch(event_tap_word)
|
|
||||||
print(f" Tap {i+1}: {response.action} - Word spacing: {response.data.get('word_spacing', 'N/A')}")
|
|
||||||
|
|
||||||
# Get updated overlay image after each tap
|
|
||||||
updated_overlay4 = reader.get_current_page()
|
|
||||||
annotated = add_gesture_annotation(
|
|
||||||
updated_overlay4,
|
|
||||||
f"Word Spacing: {reader.page_style.word_spacing}px (tap {i+1}/5)",
|
|
||||||
"top"
|
|
||||||
)
|
|
||||||
frames.append(annotated)
|
|
||||||
frame_duration.append(800) # 0.8 seconds per tap
|
|
||||||
|
|
||||||
# Hold on final word spacing for a bit longer
|
|
||||||
final_word_overlay = reader.get_current_page()
|
|
||||||
annotated_final_ws = add_gesture_annotation(
|
|
||||||
final_word_overlay,
|
|
||||||
f"Word Spacing: {reader.page_style.word_spacing}px (complete)",
|
|
||||||
"top"
|
|
||||||
)
|
|
||||||
frames.append(annotated_final_ws)
|
|
||||||
frame_duration.append(1500) # 1.5 seconds to see the final result
|
|
||||||
else:
|
|
||||||
print(" Skipping - button not found")
|
|
||||||
|
|
||||||
# Frame Z: Tap outside to close
|
# Frame Z: Tap outside to close
|
||||||
print("Frame Z: Close overlay...")
|
print("Frame Z: Close overlay...")
|
||||||
final_overlay_state = reader.get_current_page()
|
final_overlay_state = reader.get_current_page()
|
||||||
@ -421,7 +378,7 @@ def main():
|
|||||||
final_page = reader.get_current_page()
|
final_page = reader.get_current_page()
|
||||||
annotated_final = add_gesture_annotation(
|
annotated_final = add_gesture_annotation(
|
||||||
final_page,
|
final_page,
|
||||||
f"Settings Applied: {int(reader.base_font_scale * 100)}% font, {reader.page_style.line_spacing}px line, {reader.page_style.inter_block_spacing}px para, {reader.page_style.word_spacing}px word",
|
f"Settings Applied: {int(reader.base_font_scale * 100)}% font, {reader.page_style.line_spacing}px line, {reader.page_style.inter_block_spacing}px para",
|
||||||
"top"
|
"top"
|
||||||
)
|
)
|
||||||
frames.append(annotated_final)
|
frames.append(annotated_final)
|
||||||
|
|||||||
216
examples/html_generation_demo.py
Normal file
216
examples/html_generation_demo.py
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
"""
|
||||||
|
Demonstration of HTML generation for dreader UI.
|
||||||
|
|
||||||
|
This example shows how to:
|
||||||
|
1. Scan a book directory
|
||||||
|
2. Generate library view HTML
|
||||||
|
3. Load a book and generate reader view HTML
|
||||||
|
4. Generate overlay HTML for settings, TOC, and bookmarks
|
||||||
|
|
||||||
|
The generated HTML strings can be passed to a HAL (Hardware Abstraction Layer)
|
||||||
|
for rendering on the target device.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from dreader import create_ebook_reader
|
||||||
|
from dreader.html_generator import (
|
||||||
|
generate_library_html,
|
||||||
|
generate_reader_html,
|
||||||
|
generate_settings_overlay,
|
||||||
|
generate_toc_overlay,
|
||||||
|
generate_bookmarks_overlay
|
||||||
|
)
|
||||||
|
from dreader.book_utils import (
|
||||||
|
scan_book_directory,
|
||||||
|
get_chapter_list,
|
||||||
|
get_bookmark_list,
|
||||||
|
page_image_to_base64
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def demo_library_view():
|
||||||
|
"""Generate and save library view HTML."""
|
||||||
|
print("Generating library view...")
|
||||||
|
|
||||||
|
# Scan books directory
|
||||||
|
books_dir = Path('tests/data')
|
||||||
|
books = scan_book_directory(books_dir)
|
||||||
|
|
||||||
|
print(f"Found {len(books)} books:")
|
||||||
|
for book in books:
|
||||||
|
print(f" - {book['title']} by {book['author']}")
|
||||||
|
|
||||||
|
# Generate HTML
|
||||||
|
library_html = generate_library_html(books)
|
||||||
|
|
||||||
|
# Save to file for inspection
|
||||||
|
output_path = Path('output/library.html')
|
||||||
|
output_path.parent.mkdir(exist_ok=True)
|
||||||
|
output_path.write_text(library_html)
|
||||||
|
|
||||||
|
print(f"Library HTML saved to: {output_path}")
|
||||||
|
print(f"HTML length: {len(library_html)} characters\n")
|
||||||
|
|
||||||
|
|
||||||
|
def demo_reader_view():
|
||||||
|
"""Generate and save reader view HTML."""
|
||||||
|
print("Generating reader view...")
|
||||||
|
|
||||||
|
# Load a test book
|
||||||
|
book_path = Path('tests/data/test.epub')
|
||||||
|
if not book_path.exists():
|
||||||
|
print(f"Test book not found at {book_path}")
|
||||||
|
return
|
||||||
|
|
||||||
|
reader = create_ebook_reader(page_size=(800, 1000))
|
||||||
|
reader.load_epub(str(book_path))
|
||||||
|
|
||||||
|
# Get current page
|
||||||
|
page_image = reader.get_current_page()
|
||||||
|
page_base64 = page_image_to_base64(page_image)
|
||||||
|
|
||||||
|
# Generate HTML
|
||||||
|
reader_html = generate_reader_html(
|
||||||
|
book_title=reader.book_title or "Unknown Title",
|
||||||
|
book_author=reader.book_author or "Unknown Author",
|
||||||
|
page_image_data=page_base64
|
||||||
|
)
|
||||||
|
|
||||||
|
# Save to file
|
||||||
|
output_path = Path('output/reader.html')
|
||||||
|
output_path.write_text(reader_html)
|
||||||
|
|
||||||
|
print(f"Reader HTML saved to: {output_path}")
|
||||||
|
print(f"HTML length: {len(reader_html)} characters")
|
||||||
|
print(f"Book: {reader.book_title} by {reader.book_author}\n")
|
||||||
|
|
||||||
|
|
||||||
|
def demo_overlays():
|
||||||
|
"""Generate and save overlay HTML."""
|
||||||
|
print("Generating overlay views...")
|
||||||
|
|
||||||
|
# Load a test book
|
||||||
|
book_path = Path('tests/data/test.epub')
|
||||||
|
if not book_path.exists():
|
||||||
|
print(f"Test book not found at {book_path}")
|
||||||
|
return
|
||||||
|
|
||||||
|
reader = create_ebook_reader(page_size=(800, 1000))
|
||||||
|
reader.load_epub(str(book_path))
|
||||||
|
|
||||||
|
# 1. Settings overlay
|
||||||
|
settings_html = generate_settings_overlay()
|
||||||
|
settings_path = Path('output/overlay_settings.html')
|
||||||
|
settings_path.write_text(settings_html)
|
||||||
|
print(f"Settings overlay saved to: {settings_path}")
|
||||||
|
|
||||||
|
# 2. TOC overlay
|
||||||
|
chapters = get_chapter_list(reader)
|
||||||
|
print(f"Found {len(chapters)} chapters")
|
||||||
|
toc_html = generate_toc_overlay(chapters)
|
||||||
|
toc_path = Path('output/overlay_toc.html')
|
||||||
|
toc_path.write_text(toc_html)
|
||||||
|
print(f"TOC overlay saved to: {toc_path}")
|
||||||
|
|
||||||
|
# 3. Bookmarks overlay (create some test bookmarks first)
|
||||||
|
reader.save_position('Chapter 1 Start')
|
||||||
|
reader.next_page()
|
||||||
|
reader.next_page()
|
||||||
|
reader.save_position('Page 3')
|
||||||
|
|
||||||
|
bookmarks = get_bookmark_list(reader)
|
||||||
|
print(f"Found {len(bookmarks)} bookmarks")
|
||||||
|
bookmarks_html = generate_bookmarks_overlay(bookmarks)
|
||||||
|
bookmarks_path = Path('output/overlay_bookmarks.html')
|
||||||
|
bookmarks_path.write_text(bookmarks_html)
|
||||||
|
print(f"Bookmarks overlay saved to: {bookmarks_path}\n")
|
||||||
|
|
||||||
|
|
||||||
|
def demo_hal_usage():
|
||||||
|
"""
|
||||||
|
Demonstrate how a HAL would use these functions.
|
||||||
|
|
||||||
|
This simulates how your Hardware Abstraction Layer would
|
||||||
|
interact with the HTML generation functions.
|
||||||
|
"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("HAL Integration Example")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
# Step 1: Initialize with books directory
|
||||||
|
books_dir = Path('tests/data')
|
||||||
|
|
||||||
|
# Step 2: Show library view
|
||||||
|
books = scan_book_directory(books_dir)
|
||||||
|
library_html = generate_library_html(books)
|
||||||
|
print(f"\n[HAL] Rendering library with {len(books)} books")
|
||||||
|
print(f"[HAL] HTML size: {len(library_html)} bytes")
|
||||||
|
|
||||||
|
# Step 3: User selects a book (simulated)
|
||||||
|
if books:
|
||||||
|
selected_book = books[0]
|
||||||
|
print(f"\n[HAL] User selected: {selected_book['title']}")
|
||||||
|
|
||||||
|
# Step 4: Load book and show reader view
|
||||||
|
reader = create_ebook_reader(page_size=(800, 1000))
|
||||||
|
reader.load_epub(selected_book['path'])
|
||||||
|
|
||||||
|
page_image = reader.get_current_page()
|
||||||
|
page_base64 = page_image_to_base64(page_image)
|
||||||
|
|
||||||
|
reader_html = generate_reader_html(
|
||||||
|
book_title=reader.book_title,
|
||||||
|
book_author=reader.book_author,
|
||||||
|
page_image_data=page_base64
|
||||||
|
)
|
||||||
|
print(f"[HAL] Rendering reader view")
|
||||||
|
print(f"[HAL] HTML size: {len(reader_html)} bytes")
|
||||||
|
|
||||||
|
# Step 5: User presses "Settings" button (simulated)
|
||||||
|
print(f"\n[HAL] User pressed 'Settings' button")
|
||||||
|
settings_html = generate_settings_overlay()
|
||||||
|
print(f"[HAL] Rendering settings overlay on top of page")
|
||||||
|
print(f"[HAL] HTML size: {len(settings_html)} bytes")
|
||||||
|
|
||||||
|
# Step 6: User presses "Contents" button (simulated)
|
||||||
|
print(f"\n[HAL] User pressed 'Contents' button")
|
||||||
|
chapters = get_chapter_list(reader)
|
||||||
|
toc_html = generate_toc_overlay(chapters)
|
||||||
|
print(f"[HAL] Rendering TOC overlay with {len(chapters)} chapters")
|
||||||
|
print(f"[HAL] HTML size: {len(toc_html)} bytes")
|
||||||
|
|
||||||
|
# Step 7: User navigates pages (simulated)
|
||||||
|
print(f"\n[HAL] User pressed 'Next' button")
|
||||||
|
reader.next_page()
|
||||||
|
page_image = reader.get_current_page()
|
||||||
|
page_base64 = page_image_to_base64(page_image)
|
||||||
|
reader_html = generate_reader_html(
|
||||||
|
book_title=reader.book_title,
|
||||||
|
book_author=reader.book_author,
|
||||||
|
page_image_data=page_base64
|
||||||
|
)
|
||||||
|
print(f"[HAL] Rendering next page")
|
||||||
|
print(f"[HAL] HTML size: {len(reader_html)} bytes")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("dreader HTML Generation Demo")
|
||||||
|
print("=" * 60)
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Create output directory
|
||||||
|
Path('output').mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
# Run demos
|
||||||
|
demo_library_view()
|
||||||
|
demo_reader_view()
|
||||||
|
demo_overlays()
|
||||||
|
demo_hal_usage()
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
print("Demo complete!")
|
||||||
|
print("=" * 60)
|
||||||
|
print("\nGenerated HTML files have been saved to the 'output/' directory.")
|
||||||
|
print("Open them in a browser to see how they look.")
|
||||||
|
print("\nIn a real application, these HTML strings would be passed")
|
||||||
|
print("to your HAL for rendering on the target display device.")
|
||||||
Loading…
x
Reference in New Issue
Block a user