Eureka/tools/bloom-sheet/test_visitors.py
Eureka a1b19054bb bloom-sheet: a large bare fill is a wash and a heavy line, not a solid
Flat solids read as stickers beside the plant. Small fills stay solid, like a
flower's centre, and dark fills stay cutouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 15:53:43 +01:00

71 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""What a visitor must be. Run: python3 test_visitors.py
Needs python3-svgelements and python3-shapely; no LaTeX."""
import math, sys, tempfile
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from bloom_sheet import read_visitor
fails = 0
tmp = Path(tempfile.mkdtemp())
def svg(body):
f = tmp / "v.svg"
f.write_text(f'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100">{body}</svg>')
return str(f)
def check(name, ok):
global fails
print(("ok " if ok else "FAIL"), name)
fails += not ok
def refused(name, body, reason):
try:
read_visitor(svg(body))
check(f"{name}: refused", False)
except SystemExit as e:
check(f"{name}: refused for {reason}", reason in str(e))
DISC = '<circle cx="50" cy="50" r="40" fill="#f80"/>'
refused("apart", '<circle cx="20" cy="20" r="8" fill="#f80"/><circle cx="80" cy="80" r="8" fill="#f80"/>', "2 separate")
refused("apart lines", '<path d="M5 5 L20 5 M5 90 L20 90" fill="none" stroke="#f80"/>', "2 separate")
refused("gradient", '<circle cx="50" cy="50" r="40" fill="url(#g)"/>', "gradient")
refused("gradient stroke", '<circle cx="50" cy="50" r="40" fill="none" stroke="url(#g)"/>', "gradient")
refused("text", DISC + '<text x="5" y="5">hi</text>', "text")
refused("image", DISC + '<image width="9" height="9" xlink:href="data:image/png;base64,iVBORw0KGgo="/>', "image")
refused("nothing", '<circle cx="50" cy="50" r="40" fill="none"/>', "fill or a stroke")
m = read_visitor(svg(DISC))
check("a large bare fill is outlined, not solid", len(m) == 1 and m[0]["outlined"] and m[0]["colour"] == "neonorange")
m = read_visitor(svg('<path d="M10 10 L90 10 L50 90" fill="none" stroke="#59B7FF" stroke-width="2"/>'))
check("a stroke is an open line, no wash", len(m) == 1 and "pts" in m[0] and not m[0]["closed"] and m[0]["wash"] is None)
check("the line keeps its colour", m[0]["colour"] == "neonsky")
check("the stroke width scales with the drawing", 0 < m[0]["width"] < 0.1)
m = read_visitor(svg('<circle cx="50" cy="50" r="40" fill="#8F84FF" stroke="#FF8A2A" stroke-width="1"/>'))
check("a fill under a stroke is a wash", m[0]["closed"] and m[0]["wash"] == "neonviolet" and m[0]["colour"] == "neonorange")
m = read_visitor(svg('<path d="M50 5 L95 95 L5 95Z"/>'))
check("an all-black icon is seen", m[0]["colour"] == "neonwhite")
m = read_visitor(svg(DISC + '<circle cx="35" cy="35" r="5" fill="#07111A"/>'))
check("black on colour is a cutout", m[1]["colour"] == "ground" and not m[1]["outlined"])
m = read_visitor(svg(DISC + '<circle cx="35" cy="35" r="5" fill="#59B7FF"/>'))
check("a small bare fill stays solid", not m[1]["outlined"])
m = read_visitor(svg('<circle cx="50" cy="50" r="40" fill="#07111A"/><circle cx="50" cy="50" r="5" fill="#f80"/>'))
check("a large cutout stays a cutout", m[0]["colour"] == "ground" and not m[0]["outlined"])
m = read_visitor(svg(DISC + '<circle cx="35" cy="35" r="5" fill="#07111A"/>'))
check("paint order is kept", [x["colour"] for x in m] == ["neonorange", "ground"])
m = read_visitor(svg('<circle cx="50" cy="50" r="40" fill="none" stroke="#f80"/><circle cx="35" cy="35" r="5" fill="#fff"/><circle cx="65" cy="35" r="5" fill="#fff"/>'))
check("eyes inside an outline count as touching", len(m) == 3)
m = read_visitor(svg('<path fill-rule="evenodd" fill="#f80" d="M10 10 H90 V90 H10Z M40 40 H60 V60 H40Z"/>'))
check("a hole stays a ring of the solid", len(m) == 1 and len(m[0]["rings"]) == 2)
m = read_visitor(svg('<g transform="translate(30 10) scale(0.2)"><rect width="100" height="300" fill="#f80"/></g>'))
pts = [p for ring in m[0]["rings"] for p in ring]
check("any drawing lands in the unit disc", abs(max(math.hypot(*p) for p in pts) - 1) < 1e-3)
top = max(pts, key=lambda p: p[1])
check("y points up", top[1] > 0 and m[0]["rings"][0][0][1] > 0)
sys.exit(1 if fails else 0)