94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
"""Tests for A4 imposition."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
from genshin_tcg.pdf.imposition import ImpositionGrid, impose_pdf
|
|
|
|
|
|
def test_default_grid_geometry() -> None:
|
|
grid = ImpositionGrid()
|
|
assert grid.capacity == 9
|
|
assert grid.content_width_mm == pytest.approx(189.0)
|
|
assert grid.content_height_mm == pytest.approx(264.0)
|
|
assert grid.offset_x_mm == pytest.approx(10.5)
|
|
assert grid.offset_y_mm == pytest.approx(16.5)
|
|
|
|
|
|
def test_cell_positions() -> None:
|
|
grid = ImpositionGrid()
|
|
assert grid.cell_top_left_mm(0) == pytest.approx((10.5, 16.5))
|
|
assert grid.cell_top_left_mm(2) == pytest.approx((10.5 + 2 * 63, 16.5))
|
|
assert grid.cell_top_left_mm(3) == pytest.approx((10.5, 16.5 + 88))
|
|
assert grid.cell_top_left_mm(8) == pytest.approx((10.5 + 2 * 63, 16.5 + 2 * 88))
|
|
|
|
|
|
def test_cell_index_bounds() -> None:
|
|
grid = ImpositionGrid()
|
|
with pytest.raises(IndexError):
|
|
grid.cell_top_left_mm(9)
|
|
|
|
|
|
def test_page_count() -> None:
|
|
grid = ImpositionGrid()
|
|
assert grid.page_count(0) == 0
|
|
assert grid.page_count(1) == 1
|
|
assert grid.page_count(9) == 1
|
|
assert grid.page_count(10) == 2
|
|
assert grid.page_count(18) == 2
|
|
assert grid.page_count(19) == 3
|
|
|
|
|
|
def test_grid_too_large_raises() -> None:
|
|
with pytest.raises(ValueError):
|
|
ImpositionGrid(page_width_mm=100.0)
|
|
|
|
|
|
def test_explicit_margin() -> None:
|
|
grid = ImpositionGrid(margin_mm=5.0)
|
|
assert grid.offset_x_mm == 5.0
|
|
assert grid.offset_y_mm == 5.0
|
|
|
|
|
|
def test_gutter_shifts_cells() -> None:
|
|
grid = ImpositionGrid(gutter_mm=2.0, margin_mm=1.0)
|
|
assert grid.content_width_mm == pytest.approx(3 * 63 + 2 * 2)
|
|
assert grid.cell_top_left_mm(1) == pytest.approx((1.0 + 65.0, 1.0))
|
|
|
|
|
|
def _make_pngs(directory: Path, count: int) -> list[Path]:
|
|
directory.mkdir(parents=True, exist_ok=True)
|
|
paths: list[Path] = []
|
|
for index in range(count):
|
|
path = directory / f"card_{index}.png"
|
|
Image.new("RGB", (74, 104), (index * 10 % 256, 0, 0)).save(path)
|
|
paths.append(path)
|
|
return paths
|
|
|
|
|
|
def test_impose_pdf_writes_multiple_pages(tmp_path: Path) -> None:
|
|
images = _make_pngs(tmp_path / "png", 10)
|
|
output = tmp_path / "out.pdf"
|
|
result = impose_pdf(images, output, ImpositionGrid())
|
|
assert result.pages == 2
|
|
assert result.cards == 10
|
|
assert output.is_file()
|
|
assert output.stat().st_size > 1000
|
|
|
|
|
|
def test_impose_pdf_single_page(tmp_path: Path) -> None:
|
|
images = _make_pngs(tmp_path / "png", 3)
|
|
output = tmp_path / "out.pdf"
|
|
result = impose_pdf(images, output, ImpositionGrid(cut_marks=False))
|
|
assert result.pages == 1
|
|
assert output.is_file()
|
|
|
|
|
|
def test_impose_pdf_missing_image_raises(tmp_path: Path) -> None:
|
|
with pytest.raises(FileNotFoundError):
|
|
impose_pdf([tmp_path / "nope.png"], tmp_path / "out.pdf", ImpositionGrid())
|