Planches de badges centrées, verso aligné sur le recto
ci-cd / build-backend (push) Successful in 57s
ci-cd / build-frontend (push) Successful in 9s
ci-cd / deploy (push) Successful in 16s

Espace libre réparti à parts égales (mêmes écarts à gauche, entre les
badges et à droite ; en haut, entre les rangées et en bas) : grille
symétrique, le verso imprimé en miroir tombe exactement derrière le recto
au lieu d'être décalé de l'espace restant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AdamThomas
2026-10-29 16:50:59 +01:00
co-authored by Claude Opus 5.5
parent 05cdb0276b
commit 3754cfc69a
3 changed files with 72 additions and 3 deletions
+38
View File
@@ -2,6 +2,7 @@
import io
import pytest
from PIL import Image
from reportlab.lib.units import mm
from reportlab.pdfbase.pdfmetrics import stringWidth
@@ -18,6 +19,7 @@ from app.badges.pdf import (
_column_widths,
_fit_photo,
_grid_size,
_spacing,
generate_badge_sheets_pdf,
image_size_mm,
wrap_text,
@@ -396,3 +398,39 @@ def test_oval_photo_and_missing_oval_photo_generate_a_valid_pdf():
)
pdf = generate_badge_sheets_pdf([badge], None, None)
assert pdf.startswith(b"%PDF")
@pytest.mark.parametrize(
"size", [(DEFAULT_BADGE_WIDTH_MM, DEFAULT_BADGE_HEIGHT_MM), (54, 85.6), (50, 50), (74, 105)]
)
def test_grid_is_centred_with_equal_gaps_and_back_falls_behind_front(size):
from app.badges.pdf import _PAGE_HEIGHT_MM, _PAGE_WIDTH_MM
w, h = size
cols, rows = _grid_size(w, h)
xs = sorted({_cell_origin(i, cols, w, h)[0] for i in range(cols * rows)})
ys = sorted({_cell_origin(i, cols, w, h)[1] for i in range(cols * rows)})
left, right = xs[0], _PAGE_WIDTH_MM - (xs[-1] + w)
bottom, top = ys[0], _PAGE_HEIGHT_MM - (ys[-1] + h)
# Symétrique : même marge à gauche et à droite, en haut et en bas.
assert left == pytest.approx(right)
assert top == pytest.approx(bottom)
# Écarts entre badges tous égaux, et égaux aux marges quand c'est possible.
gaps_x = {round(b - a - w, 6) for a, b in zip(xs, xs[1:], strict=False)}
gaps_y = {round(b - a - h, 6) for a, b in zip(ys, ys[1:], strict=False)}
assert len(gaps_x) <= 1 and len(gaps_y) <= 1
_, gap_x = _spacing(cols, _PAGE_WIDTH_MM, w)
if gap_x >= 10 and cols > 1:
assert left == pytest.approx(gap_x)
# Verso : exactement derrière le recto une fois la feuille retournée
# sur son bord long (miroir horizontal).
for i in range(cols * rows):
fx, fy = _cell_origin(i, cols, w, h)
bx, by = _cell_origin(i, cols, w, h, mirrored=True)
assert bx == pytest.approx(_PAGE_WIDTH_MM - fx - w)
assert by == pytest.approx(fy)
def test_cr80_gaps_are_even():
# 2 × 85,6 mm sur 210 mm : 12,9 mm à gauche, entre les deux, à droite.
assert _spacing(2, 210, 85.6) == pytest.approx((12.9333, 12.9333), abs=1e-3)