debug print
This commit is contained in:
@@ -8,7 +8,6 @@ let
|
|||||||
priority = toString (containerCfg.extra.priority or 2147482647);
|
priority = toString (containerCfg.extra.priority or 2147482647);
|
||||||
logoSvgFileName = builtins.baseNameOf (toString mediaCfg.logo.svg);
|
logoSvgFileName = builtins.baseNameOf (toString mediaCfg.logo.svg);
|
||||||
logoSvgMount = "/assets/${logoSvgFileName}";
|
logoSvgMount = "/assets/${logoSvgFileName}";
|
||||||
borderRadius = toString (containerCfg.extra.borderRadius or 16);
|
|
||||||
ensureAttrSet = field: value:
|
ensureAttrSet = field: value:
|
||||||
if builtins.isAttrs value then
|
if builtins.isAttrs value then
|
||||||
value
|
value
|
||||||
@@ -73,7 +72,7 @@ let
|
|||||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
|
||||||
import cairosvg
|
import cairosvg
|
||||||
from PIL import Image, ImageDraw
|
from PIL import Image
|
||||||
|
|
||||||
LOGO_PATH = ${builtins.toJSON logoSvgMount}
|
LOGO_PATH = ${builtins.toJSON logoSvgMount}
|
||||||
LISTEN_HOST = "0.0.0.0"
|
LISTEN_HOST = "0.0.0.0"
|
||||||
@@ -114,10 +113,15 @@ let
|
|||||||
return candidates
|
return candidates
|
||||||
|
|
||||||
def _profile_for_host(host):
|
def _profile_for_host(host):
|
||||||
for candidate in _host_candidates(host):
|
candidates = _host_candidates(host)
|
||||||
|
print(f"favicon-profile host={host!r} candidates={candidates!r} mappings={list(MAPPINGS.keys())!r}")
|
||||||
|
for candidate in candidates:
|
||||||
profile = MAPPINGS.get(candidate)
|
profile = MAPPINGS.get(candidate)
|
||||||
|
print(f"favicon-profile-check candidate={candidate!r} hit={profile is not None}")
|
||||||
if profile:
|
if profile:
|
||||||
|
print(f"favicon-profile-match candidate={candidate!r} profile={profile!r}")
|
||||||
return candidate, profile
|
return candidate, profile
|
||||||
|
print(f"favicon-profile-default host={host!r} default={DEFAULT_PROFILE!r}")
|
||||||
return None, DEFAULT_PROFILE
|
return None, DEFAULT_PROFILE
|
||||||
|
|
||||||
def _replace_logo_fill(svg, color):
|
def _replace_logo_fill(svg, color):
|
||||||
@@ -128,11 +132,6 @@ let
|
|||||||
flags=re.IGNORECASE,
|
flags=re.IGNORECASE,
|
||||||
)
|
)
|
||||||
|
|
||||||
border_radius = str(${builtins.toJSON borderRadius}).strip()
|
|
||||||
if not border_radius.endswith("px"):
|
|
||||||
border_radius = f"{border_radius}px"
|
|
||||||
border_radius_px = max(0, int(float(border_radius[:-2])))
|
|
||||||
|
|
||||||
def _colors(profile):
|
def _colors(profile):
|
||||||
profile = profile or {}
|
profile = profile or {}
|
||||||
return {
|
return {
|
||||||
@@ -140,26 +139,30 @@ let
|
|||||||
"fg": profile.get("fg") or profile.get("foreground") or DEFAULT_COLORS["fg"],
|
"fg": profile.get("fg") or profile.get("foreground") or DEFAULT_COLORS["fg"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def _add_background(svg, color):
|
||||||
|
return re.sub(
|
||||||
|
r"(<svg\\b[^>]*>)",
|
||||||
|
rf'\\1<circle cx="64" cy="64" r="64" fill="{color}"/>',
|
||||||
|
svg,
|
||||||
|
count=1,
|
||||||
|
flags=re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
def _render_icon(colors):
|
def _render_icon(colors):
|
||||||
svg = LOGO_BYTES.decode("utf-8")
|
svg = LOGO_BYTES.decode("utf-8")
|
||||||
svg = _replace_logo_fill(svg, colors["fg"])
|
svg = _replace_logo_fill(svg, colors["fg"])
|
||||||
print(f"favicon-render fg={colors['fg']} bg={colors['bg']}")
|
svg = _add_background(svg, colors["bg"])
|
||||||
|
print(f"favicon-render fg={colors['fg']} bg={colors['bg']} mode=svg2png")
|
||||||
|
|
||||||
logo_png = cairosvg.svg2png(
|
png = cairosvg.svg2png(
|
||||||
bytestring=svg.encode("utf-8"),
|
bytestring=svg.encode("utf-8"),
|
||||||
output_width=ASSET_SIZE,
|
output_width=ASSET_SIZE,
|
||||||
output_height=ASSET_SIZE,
|
output_height=ASSET_SIZE,
|
||||||
)
|
)
|
||||||
output = BytesIO()
|
output = BytesIO()
|
||||||
with Image.new("RGBA", (ASSET_SIZE, ASSET_SIZE), (0, 0, 0, 0)) as canvas:
|
with Image.open(BytesIO(png)) as image:
|
||||||
ImageDraw.Draw(canvas).rounded_rectangle(
|
with image.convert("RGBA") as rgba:
|
||||||
(0, 0, ASSET_SIZE, ASSET_SIZE),
|
rgba.save(output, format="ICO", sizes=[(ASSET_SIZE, ASSET_SIZE)])
|
||||||
radius=min(border_radius_px, ASSET_SIZE // 2),
|
|
||||||
fill=colors["bg"],
|
|
||||||
)
|
|
||||||
with Image.open(BytesIO(logo_png)) as logo:
|
|
||||||
canvas.alpha_composite(logo.convert("RGBA"))
|
|
||||||
canvas.save(output, format="ICO", sizes=[(ASSET_SIZE, ASSET_SIZE)])
|
|
||||||
return output.getvalue()
|
return output.getvalue()
|
||||||
|
|
||||||
class Handler(BaseHTTPRequestHandler):
|
class Handler(BaseHTTPRequestHandler):
|
||||||
|
|||||||
Reference in New Issue
Block a user