diff --git a/scripts/generate_test_epub.py b/scripts/generate_test_epub.py index d8a7f4db..39d2dad3 100644 --- a/scripts/generate_test_epub.py +++ b/scripts/generate_test_epub.py @@ -5,6 +5,7 @@ Generate test EPUBs for rendering verification. Creates EPUBs to verify: - Image: Grayscale rendering (4 levels), scaling, centering, cache performance - Text: pre element line breaks, blank lines, nested code element +- Layout: nested block margins, sibling style restoration, image wrapper spacing """ import os @@ -46,7 +47,7 @@ def get_font(size=20): for path in candidates: try: return ImageFont.truetype(path, size) - except: + except Exception: continue return ImageFont.load_default() @@ -554,6 +555,7 @@ def create_epub(epub_path, title, chapters): # Collect all images and chapters manifest_items = [] spine_items = [] + written_images = set() # Add chapters and images for i, (chapter_title, html_content, images) in enumerate(chapters): @@ -562,6 +564,8 @@ def create_epub(epub_path, title, chapters): # Add images for this chapter for img_filename, img_data in images: + if img_filename in written_images: + continue media_type = ( "image/png" if img_filename.endswith(".png") else "image/jpeg" ) @@ -569,6 +573,7 @@ def create_epub(epub_path, title, chapters): f' ' ) epub.writestr(f"OEBPS/images/{img_filename}", img_data) + written_images.add(img_filename) # Add chapter manifest_items.append( @@ -618,12 +623,12 @@ def create_epub(epub_path, title, chapters): epub.writestr("OEBPS/nav.xhtml", nav_xhtml) -def make_chapter(title, body_content): +def make_chapter(title, body_content, head_content=""): """Create XHTML chapter content.""" return f""" -{title} +{title}{head_content}

{title}

{body_content} @@ -1001,6 +1006,134 @@ def main(): OUTPUT_DIR / "test_mixed_images.epub", "Mixed Format Tests", mixed_chapters ) + print("Creating layout regression test EPUB...") + layout_css = """ + +""" + + layout_chapters = [ + ( + "Introduction", + make_chapter( + "Layout Regression Tests", + """ +

This EPUB exercises recent parser edge cases around nested block styles and image wrappers.

+

Recommended settings: Embedded Style ON, Paragraph Alignment set to Book Style or Justify.

+ +""", + ), + [], + ), + ( + "1. Nested Horizontal Margins", + make_chapter( + "Nested Horizontal Margin Inheritance", + """ +

This chapter mirrors the c1/c2/c3/c4 example behind PR 1582.

+

Expected: the first paragraph is visibly more indented than the second, but both still inherit the outer wrapper indentation.

+
+
+

C3 paragraph. This text should have the largest left indent because it inherits c1, c2, and c3. Repeat text to make the paragraph wrap across multiple lines and make the effective left inset obvious while reading.

+
+

C4 paragraph. This text should still inherit the outer c1 indent, but not the inner c2 indent. It should therefore appear less indented than the paragraph above, not flush with the body text.

+
+

Reference paragraph outside the wrappers. This paragraph should align with the normal body text and helps compare the wrapper indentation against the baseline.

+""", + head_content=layout_css, + ), + [], + ), + ( + "2. Nested Vertical Margins", + make_chapter( + "Nested Vertical Margin Sanity", + """ +

Expected: wrapper nesting should not create an oversized blank vertical gulf between these paragraphs.

+
+
+

Nested vertical spacing paragraph. There should be some breathing room above and below, but not dramatically more than a normal section break.

+
+

Sibling paragraph after the nested block. Spacing before this paragraph should feel normal and should not keep growing with every ancestor wrapper.

+
+

Baseline paragraph after the wrapper section. This should not be pushed far down the page.

+""", + head_content=layout_css, + ), + [], + ), + ( + "3. Image Wrapper Spacing", + make_chapter( + "Image Wrapper Spacing", + """ +

Expected: the wrapper's margins should create space around the image, and the paragraph after the image should start with normal spacing rather than inheriting a second copy of that gap.

+
+
+

Wrapped image spacing test

+
+
+

Paragraph after wrapped image. If the wrapper spacing leaks, this paragraph will begin too far down the page. If container width is ignored, the image may also appear too wide for the wrapper.

+""", + head_content=layout_css, + ), + [("centering_test.jpg", images["centering_test.jpg"])], + ), + ( + "4. Hidden Image Spacing", + make_chapter( + "Hidden Image Spacing Reset", + """ +

Expected: the hidden image wrapper should not leave a large blank gap before the following paragraph.

+
+

This image is intentionally hidden by CSS

+
+

Paragraph after hidden image. This should follow with near-normal spacing, not the large gap that would be appropriate for a visible wrapped image.

+""", + head_content=layout_css, + ), + [("centering_test.jpg", images["centering_test.jpg"])], + ), + ] + + create_epub( + OUTPUT_DIR / "test_layout_regressions.epub", + "Layout Regression Tests", + layout_chapters, + ) + print("Creating text rendering test EPUB...") text_chapters = [ ( diff --git a/test/epubs/test_jpeg_images.epub b/test/epubs/test_jpeg_images.epub index a99251f0..5077801f 100644 Binary files a/test/epubs/test_jpeg_images.epub and b/test/epubs/test_jpeg_images.epub differ diff --git a/test/epubs/test_layout_regressions.epub b/test/epubs/test_layout_regressions.epub new file mode 100644 index 00000000..b145dd67 Binary files /dev/null and b/test/epubs/test_layout_regressions.epub differ diff --git a/test/epubs/test_mixed_images.epub b/test/epubs/test_mixed_images.epub index 6d0a41f1..008a16bd 100644 Binary files a/test/epubs/test_mixed_images.epub and b/test/epubs/test_mixed_images.epub differ diff --git a/test/epubs/test_png_images.epub b/test/epubs/test_png_images.epub index 5a405f88..32412027 100644 Binary files a/test/epubs/test_png_images.epub and b/test/epubs/test_png_images.epub differ diff --git a/test/epubs/test_text_rendering.epub b/test/epubs/test_text_rendering.epub index 2067f55c..7cf7d479 100644 Binary files a/test/epubs/test_text_rendering.epub and b/test/epubs/test_text_rendering.epub differ