TableTower/tools/theme_art/frames.py

109 lines
4.9 KiB
Python
Raw Permalink Normal View History

"""Frame sets (corner + edge, greyscale) built from a band profile, plus optional coloured corner ornaments.
A band profile is a list of Stroke layers drawn along the frame's centre line. The corner draws them along
a rounded (or square) turn; the edge draws them along a straight vertical line. Both use the same
centre line (`band_mid`) and the same layers, so the two join with no step.
"""
import math
from dataclasses import dataclass, field
from svg_common import grey, num, svg_document
SCALE = 0.25 # art is authored at 4x screen size
@dataclass
class Stroke:
"""One layer of the band: width and offset in source units (positive offset = toward the card)."""
width: float
lightness: float
opacity: float = 1.0
offset: float = 0.0
dash: str = ""
@dataclass
class FrameStyle:
band_mid: float # side_mid = top_mid = edge_mid (source units)
radius: float # turn radius of the band centre line (0 = square, mitred)
strokes: list
corner_size: tuple
edge_length: float = 64
band_inside_px: float = 0.0 # where the band centre sits, in px inside the card edge (negative = outside)
corner_extras: str = "" # extra greyscale shapes drawn on the corner
edge_extras: str = "" # extra greyscale shapes drawn on each edge tile
corner_fixed: str = "" # coloured overlay shapes (empty = no corner_fixed file)
pad_extra_px: float = 2.0
chamfer: float = 0.0 # > 0: the corner is cut on a 45 degree line this long (source units)
corner_under: str = "" # greyscale shapes drawn under the strokes (corner)
edge_under: str = "" # greyscale shapes drawn under the strokes (edge)
band_half_px: float = 0.0 # override for the band's half thickness when it is not made of strokes
highlight: float = 0.45 # how far white lightens the trim colour (gradient map)
notes: list = field(default_factory=list)
def _stroke_attributes(stroke, join):
dash = f' stroke-dasharray="{stroke.dash}"' if stroke.dash else ""
return (f'fill="none" stroke="{grey(stroke.lightness)}" stroke-width="{num(stroke.width)}" '
f'stroke-opacity="{num(stroke.opacity)}" stroke-linejoin="{join}" stroke-linecap="butt"{dash}')
def corner_path(style, offset):
"""The band's line, moved `offset` toward the card, from the corner's bottom edge to its right edge."""
width, height = style.corner_size
position = style.band_mid + offset
turn_start = style.band_mid + style.radius
turn_radius = max(style.radius - offset, 0)
if style.chamfer > 0:
diagonal = 2 * style.band_mid + style.chamfer + offset * math.sqrt(2) - position
return (f"M{num(position)},{num(height)} L{num(position)},{num(diagonal)} "
f"L{num(diagonal)},{num(position)} L{num(width)},{num(position)}")
if style.radius <= 0:
return f"M{num(position)},{num(height)} L{num(position)},{num(position)} L{num(width)},{num(position)}"
return (f"M{num(position)},{num(height)} L{num(position)},{num(turn_start)} "
f"A{num(turn_radius)},{num(turn_radius)} 0 0 1 {num(turn_start)},{num(position)} "
f"L{num(width)},{num(position)}")
def corner_svg(style):
join = "miter" if style.radius <= 0 else "round"
body = style.corner_under + "".join(f'<path d="{corner_path(style, stroke.offset)}" {_stroke_attributes(stroke, join)}/>'
for stroke in style.strokes)
width, height = style.corner_size
return svg_document(width, height, body + style.corner_extras)
def edge_size(style):
return [style.band_mid * 2, style.edge_length]
def edge_svg(style):
width, height = edge_size(style)
body = style.edge_under
for stroke in style.strokes:
position = num(style.band_mid + stroke.offset)
body += f'<path d="M{position},0 L{position},{num(height)}" {_stroke_attributes(stroke, "miter")}/>'
return svg_document(width, height, body + style.edge_extras)
def corner_fixed_svg(style):
if not style.corner_fixed:
return None
width, height = style.corner_size
return svg_document(width, height, style.corner_fixed)
def frame_meta(style):
"""The numbers the app needs to place this frame set."""
band_half_px = style.band_half_px or max(stroke.width / 2 + abs(stroke.offset) for stroke in style.strokes
if stroke.opacity >= 0.5) * SCALE
outset = style.band_mid * SCALE - style.band_inside_px
pad = max(0.0, style.band_inside_px + band_half_px) + style.pad_extra_px
return {
"corner_size": list(style.corner_size), "edge_size": edge_size(style),
"side_mid": style.band_mid, "top_mid": style.band_mid, "edge_mid": style.band_mid,
"scale": SCALE, "outset": round(outset, 2), "pad": math.ceil(pad),
"mid": 0.5, "highlight": style.highlight, "has_corner_fixed": bool(style.corner_fixed),
}