fix(renderer): imagen visual SIEMPRE llena el alto, recorta si no calza
El renderer ya hacía height-first pero caía a width-first cuando la
imagen era proporcionalmente más ancha que el área (square, landscape).
Eso dejaba el alto sin llenar. Ahora:
- PNG con aspect ≤ aspect del área:
cabe sin recortar. Alto = area_h, ancho proporcional menor que
area_w. El alignment CENTER del párrafo cubre el sobrante.
- PNG más ancho (square, landscape):
se recorta EN SITIO con Pillow, cortando los lados de forma
simétrica hasta que el aspect del PNG coincida con el del área.
Resultado: ambas dimensiones llenan el área (area_w x area_h).
Reemplaza `_calcular_dims_emu` por `_preparar_imagen_visual` (la nueva
función puede mutar el PNG). Es seguro: los PNGs viven en /tmp por
sesión y se borran al final del documento. Idempotente: una segunda
llamada sobre un PNG ya recortado no lo vuelve a tocar.
Validado local con tres PNGs (portrait, square, landscape) →
PDF de 3 páginas, todas con cy = 6_818_400 EMU = 18.94 cm exacto:
portrait: cx=5_268_763 (14.64 cm, cabe → centrado)
square: recortado 1500→1393px, cx=6_332_400 (17.59 cm, exacto)
landscape: recortado 1600→835px, cx=6_332_400 (17.59 cm, exacto)
Tests: 124 passed. +5 casos (portrait sin crop, cuadrado con crop,
landscape recorte fuerte, idempotencia, aspect exacto).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
32c1a2bc17
commit
018beb0059
2 files changed
+158
-20
No files matched your search
@@ -0,0 +1,107 @@
|
||||
"""Tests del cálculo y crop de la imagen para modo visual."""
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from valeria.renderer import _preparar_imagen_visual
|
||||
|
||||
# Área de contenido carta: 17.59 x 18.94 cm
|
||||
AREA_W_EMU = 17_590_000 * 36 // 10 # ≈ 6_332_400
|
||||
AREA_H_EMU = 18_940_000 * 36 // 10 # ≈ 6_818_400
|
||||
# Recalculo exacto:
|
||||
EMU_POR_CM = 360000
|
||||
AREA_W_EMU = int(17.59 * EMU_POR_CM) # 6_332_400
|
||||
AREA_H_EMU = int(18.94 * EMU_POR_CM) # 6_818_400
|
||||
AREA_ASPECT = AREA_W_EMU / AREA_H_EMU # ≈ 0.929
|
||||
|
||||
|
||||
def _png(tmp_path: Path, w: int, h: int, color: str = "red") -> Path:
|
||||
"""Genera un PNG sólido del tamaño dado."""
|
||||
from PIL import Image
|
||||
p = tmp_path / f"{w}x{h}.png"
|
||||
im = Image.new("RGB", (w, h), color)
|
||||
im.save(str(p), "PNG")
|
||||
return p
|
||||
|
||||
|
||||
def test_portrait_no_recorta_y_llena_alto(tmp_path: Path):
|
||||
"""PNG portrait (1275x1650, aspect 0.77) cabe → alto al máximo,
|
||||
ancho proporcional menor que el área."""
|
||||
p = _png(tmp_path, 1275, 1650)
|
||||
w_emu, h_emu = _preparar_imagen_visual(p, AREA_W_EMU, AREA_H_EMU)
|
||||
|
||||
# Alto: lleno
|
||||
assert h_emu == AREA_H_EMU
|
||||
# Ancho: proporcional, menor que el área
|
||||
assert w_emu < AREA_W_EMU
|
||||
esperado_w = (AREA_H_EMU * 1275) // 1650
|
||||
assert w_emu == esperado_w
|
||||
# PNG no debe haberse modificado en píxeles
|
||||
from PIL import Image
|
||||
with Image.open(str(p)) as im:
|
||||
assert im.size == (1275, 1650)
|
||||
|
||||
|
||||
def test_cuadrado_recorta_y_llena_ambos(tmp_path: Path):
|
||||
"""PNG 1:1 es más ancho (en proporción) que el área 0.93 → recorta."""
|
||||
p = _png(tmp_path, 1500, 1500)
|
||||
w_emu, h_emu = _preparar_imagen_visual(p, AREA_W_EMU, AREA_H_EMU)
|
||||
|
||||
# Después del recorte calza el área entera
|
||||
assert h_emu == AREA_H_EMU
|
||||
assert w_emu == AREA_W_EMU
|
||||
# PNG en disco fue recortado a la proporción del área
|
||||
from PIL import Image
|
||||
with Image.open(str(p)) as im:
|
||||
new_iw, new_ih = im.size
|
||||
assert new_ih == 1500 # alto intacto
|
||||
esperado_iw = int(1500 * AREA_ASPECT)
|
||||
assert new_iw == esperado_iw
|
||||
assert new_iw < 1500
|
||||
|
||||
|
||||
def test_landscape_recorta_fuerte(tmp_path: Path):
|
||||
"""PNG 16:9 (aspect 1.78) muy ancho → recorta agresivo."""
|
||||
p = _png(tmp_path, 1600, 900)
|
||||
w_emu, h_emu = _preparar_imagen_visual(p, AREA_W_EMU, AREA_H_EMU)
|
||||
assert h_emu == AREA_H_EMU
|
||||
assert w_emu == AREA_W_EMU
|
||||
from PIL import Image
|
||||
with Image.open(str(p)) as im:
|
||||
new_iw, _ = im.size
|
||||
esperado = int(900 * AREA_ASPECT) # ~836
|
||||
assert new_iw == esperado
|
||||
assert new_iw < 1600
|
||||
|
||||
|
||||
def test_idempotente_segunda_llamada_no_recorta(tmp_path: Path):
|
||||
"""Una imagen ya recortada no debe mutarse en sitio una segunda vez."""
|
||||
p = _png(tmp_path, 1500, 1500)
|
||||
_preparar_imagen_visual(p, AREA_W_EMU, AREA_H_EMU)
|
||||
from PIL import Image
|
||||
with Image.open(str(p)) as im:
|
||||
size_post_1 = im.size
|
||||
|
||||
w_emu, h_emu = _preparar_imagen_visual(p, AREA_W_EMU, AREA_H_EMU)
|
||||
# El alto sigue lleno; el ancho puede quedar 1-2 EMU por debajo de
|
||||
# AREA_W_EMU por floor() en el crop original. Lo importante: el
|
||||
# archivo no se vuelve a tocar.
|
||||
assert h_emu == AREA_H_EMU
|
||||
assert abs(w_emu - AREA_W_EMU) < AREA_W_EMU // 100 # < 1%
|
||||
with Image.open(str(p)) as im:
|
||||
size_post_2 = im.size
|
||||
assert size_post_2 == size_post_1
|
||||
|
||||
|
||||
def test_exactamente_aspect_del_area_no_recorta(tmp_path: Path):
|
||||
"""Si el PNG ya tiene exactamente el aspect del área, no se toca."""
|
||||
# Construyo un PNG con exactamente el aspect del área
|
||||
ih = 1000
|
||||
iw = int(ih * AREA_ASPECT)
|
||||
p = _png(tmp_path, iw, ih)
|
||||
w_emu, h_emu = _preparar_imagen_visual(p, AREA_W_EMU, AREA_H_EMU)
|
||||
assert h_emu == AREA_H_EMU
|
||||
# w_emu puede ser ≤ AREA_W_EMU según camino tomado
|
||||
from PIL import Image
|
||||
with Image.open(str(p)) as im:
|
||||
assert im.size == (iw, ih)
|
||||
Reference in new issue
Block a user