From fed3296f3d2b54216630d39489fbc5623b3cc100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Degros?= <57880064+fdegros@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:15:54 +1100 Subject: [PATCH] Avoid ZeroDivisionError when a component is wider than the page When a card or wallet is wider (or taller) than the printable page, layout_components computed 0 components per page, and page_xy divided by zero. Lay out at least 1 component per page in each direction instead; the component simply overflows the page margins. See #10 (the error reported with --paper a4 and the wider half-, third- and quarter-letter formats). --- slip39/layout/components.py | 4 ++-- slip39/output_test.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/slip39/layout/components.py b/slip39/layout/components.py index 61f770ae..3f6fbd3f 100644 --- a/slip39/layout/components.py +++ b/slip39/layout/components.py @@ -586,8 +586,8 @@ def layout_components( if bleed is None: bleed = 5/100 # FPDF().epw/.eph is *without* page margins, but *with* re-orienting for portrait/landscape - comp_cols = int(( pdf.epw - page_margin_mm * 2 * min( 1, 1 - bleed )) // comp_dim.x ) - comp_rows = int(( pdf.eph - page_margin_mm * 2 * min( 1, 1 - bleed )) // comp_dim.y ) + comp_cols = max( 1, int(( pdf.epw - page_margin_mm * 2 * min( 1, 1 - bleed )) // comp_dim.x )) + comp_rows = max( 1, int(( pdf.eph - page_margin_mm * 2 * min( 1, 1 - bleed )) // comp_dim.y )) comps_pp = comp_rows * comp_cols # Compute actual page margins to center cards. diff --git a/slip39/output_test.py b/slip39/output_test.py index 903c0b4e..c496d465 100644 --- a/slip39/output_test.py +++ b/slip39/output_test.py @@ -4,6 +4,7 @@ from fpdf import FPDF, FlexTemplate from .layout import Region, Text, Image, Box, Coordinate +from .layout.components import layout_components from .defaults import MM_IN @@ -73,3 +74,13 @@ def test_Region(): tpl.render( offsetx = card_size.x * MM_IN, offsety = card_size.y * MM_IN ) #pdf.output( "test.pdf" ) # To view results in test.pdf, uncomment + + +def test_layout_components_oversize(): + """A component larger than the page is laid out 1 per page, instead of raising ZeroDivisionError.""" + pdf = FPDF( orientation='portrait', format='A4' ) + pdf.add_page() + comps_pp,page_xy = layout_components( pdf, comp_dim=Coordinate( x=pdf.epw * 2, y=pdf.eph * 2 )) + assert comps_pp == 1 + page,_ = page_xy( 3 ) + assert page == 3