fix cover issue, add copyleft text
This commit is contained in:
@@ -79,6 +79,11 @@ class RenderableImage(Renderable, Queriable):
|
||||
def _load_image(self):
|
||||
"""Load the image from the source path"""
|
||||
try:
|
||||
# Check if the image has already been loaded into memory
|
||||
if hasattr(self._abstract_image, '_loaded_image') and self._abstract_image._loaded_image is not None:
|
||||
self._pil_image = self._abstract_image._loaded_image
|
||||
return
|
||||
|
||||
source = self._abstract_image.source
|
||||
|
||||
# Handle different types of sources
|
||||
|
||||
@@ -194,6 +194,9 @@ class Page(Renderable, Queriable):
|
||||
# Synchronize draw context for Line objects before rendering
|
||||
if hasattr(child, '_draw'):
|
||||
child._draw = self._draw
|
||||
# Synchronize canvas for Image objects before rendering
|
||||
if hasattr(child, '_canvas'):
|
||||
child._canvas = self._canvas
|
||||
if hasattr(child, 'render'):
|
||||
child.render()
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ class EPUBReader:
|
||||
self.toc = []
|
||||
self.spine = []
|
||||
self.manifest = {}
|
||||
self.cover_id = None # ID of the cover image in manifest
|
||||
|
||||
def read(self) -> Book:
|
||||
"""
|
||||
@@ -172,6 +173,15 @@ class EPUBReader:
|
||||
else:
|
||||
# Store other metadata
|
||||
self.metadata[name] = value
|
||||
|
||||
# Parse meta elements for cover reference
|
||||
for meta in metadata_elem.findall('.//{{{0}}}meta'.format(NAMESPACES['opf'])):
|
||||
name = meta.get('name')
|
||||
content = meta.get('content')
|
||||
|
||||
if name == 'cover' and content:
|
||||
# This is a reference to the cover image in the manifest
|
||||
self.cover_id = content
|
||||
|
||||
def _parse_manifest(self, root: ET.Element):
|
||||
"""
|
||||
@@ -320,8 +330,67 @@ class EPUBReader:
|
||||
if 'publisher' in self.metadata:
|
||||
self.book.set_metadata(MetadataType.PUBLISHER, self.metadata['publisher'])
|
||||
|
||||
def _add_cover_chapter(self):
|
||||
"""Add a cover chapter if a cover image is available."""
|
||||
if not self.cover_id or self.cover_id not in self.manifest:
|
||||
return
|
||||
|
||||
# Get the cover image path from the manifest
|
||||
cover_item = self.manifest[self.cover_id]
|
||||
cover_path = cover_item['path']
|
||||
|
||||
# Check if the file exists
|
||||
if not os.path.exists(cover_path):
|
||||
print(f"Warning: Cover image file not found: {cover_path}")
|
||||
return
|
||||
|
||||
# Create a cover chapter
|
||||
cover_chapter = self.book.create_chapter("Cover", 0)
|
||||
|
||||
try:
|
||||
# Create an Image block for the cover
|
||||
from pyWebLayout.abstract.block import Image as AbstractImage
|
||||
from PIL import Image as PILImage
|
||||
import io
|
||||
|
||||
# Load the image into memory before the temp directory is cleaned up
|
||||
# We need to fully copy the image data to ensure it persists after temp cleanup
|
||||
with open(cover_path, 'rb') as f:
|
||||
image_bytes = f.read()
|
||||
|
||||
# Create PIL image from bytes in memory
|
||||
pil_image = PILImage.open(io.BytesIO(image_bytes))
|
||||
pil_image.load() # Force loading into memory
|
||||
|
||||
# Create a copy to ensure all data is in memory
|
||||
pil_image = pil_image.copy()
|
||||
|
||||
# Create an AbstractImage block with the cover image path
|
||||
cover_image = AbstractImage(source=cover_path, alt_text="Cover Image")
|
||||
|
||||
# Set dimensions from the loaded image
|
||||
cover_image._width = pil_image.width
|
||||
cover_image._height = pil_image.height
|
||||
|
||||
# Store the loaded PIL image in the abstract image so it persists after temp cleanup
|
||||
cover_image._loaded_image = pil_image
|
||||
|
||||
# Add the image to the cover chapter
|
||||
cover_chapter.add_block(cover_image)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error creating cover chapter: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
# If we can't create the cover image, remove the chapter
|
||||
if hasattr(self.book, 'chapters') and cover_chapter in self.book.chapters:
|
||||
self.book.chapters.remove(cover_chapter)
|
||||
|
||||
def _add_chapters(self):
|
||||
"""Add chapters to the book based on the spine and TOC."""
|
||||
# Add cover chapter first if available
|
||||
self._add_cover_chapter()
|
||||
|
||||
# Create a mapping from src to TOC entry
|
||||
toc_map = {}
|
||||
|
||||
@@ -340,7 +409,8 @@ class EPUBReader:
|
||||
add_to_toc_map(self.toc)
|
||||
|
||||
# Process spine items
|
||||
chapter_index = 0 # Keep track of actual content chapters
|
||||
# Start from chapter_index = 1 if cover was added, otherwise 0
|
||||
chapter_index = 1 if (self.cover_id and self.cover_id in self.manifest) else 0
|
||||
for i, idref in enumerate(self.spine):
|
||||
if idref not in self.manifest:
|
||||
continue
|
||||
|
||||
@@ -244,9 +244,12 @@ def image_layouter(image: AbstractImage, page: Page, max_width: Optional[int] =
|
||||
x_offset = page.border_size
|
||||
y_offset = page._current_y_offset
|
||||
|
||||
# Access page.draw to ensure canvas is initialized
|
||||
_ = page.draw
|
||||
|
||||
renderable_image = RenderableImage(
|
||||
image=image,
|
||||
canvas=page.canvas,
|
||||
canvas=page._canvas,
|
||||
max_width=max_width,
|
||||
max_height=max_height,
|
||||
origin=(x_offset, y_offset),
|
||||
|
||||
Reference in New Issue
Block a user