feat(cover): pixel-width headline truncation and larger font

Replace character-count truncation (45 chars) with Pillow textbbox
pixel-width measurement. Bump headline font 14→18, line spacing 22→26.

Made-with: Cursor
This commit is contained in:
cottongin
2026-04-06 18:58:42 -04:00
parent d6cef67420
commit 9a8b586292
2 changed files with 50 additions and 6 deletions

View File

@@ -3,13 +3,15 @@ from collections import Counter
from datetime import date
from unittest.mock import patch, MagicMock
from io import BytesIO
from PIL import Image as PILImage
from PIL import Image as PILImage, ImageDraw, ImageFont
from src.cover import (
generate_programmatic_cover,
generate_ai_cover,
generate_cover,
_get_dominant_category,
_truncate_to_width,
_get_font,
CATEGORY_PROMPTS,
)
@@ -109,3 +111,28 @@ def test_get_dominant_category():
def test_category_prompts_include_massachusetts():
for category, prompt in CATEGORY_PROMPTS.items():
assert "Massachusetts" in prompt, f"Prompt for {category} missing 'Massachusetts'"
def test_truncate_to_width_respects_pixel_budget():
font = _get_font(18)
short = "Short text"
result = _truncate_to_width(short, font, 400)
assert result == short, "Short text should not be truncated"
long = "A" * 200
result = _truncate_to_width(long, font, 400)
assert result.endswith("\u2026"), "Long text should end with ellipsis"
assert len(result) < len(long), "Truncated text should be shorter"
def test_truncate_to_width_fits_within_budget():
font = _get_font(18)
long = "This is a really long headline that should definitely be truncated to fit"
max_width = 448
result = _truncate_to_width(long, font, max_width)
dummy = PILImage.new("RGBA", (1, 1))
draw = ImageDraw.Draw(dummy)
bbox = draw.textbbox((0, 0), result, font=font)
rendered_width = bbox[2] - bbox[0]
assert rendered_width <= max_width, f"Rendered width {rendered_width} exceeds {max_width}"