forked from genewildish/Mainline
refactor(legacy): Create engine/legacy/ subsystem and move render/layers (Phase 3.2)
- Create engine/legacy/ package for deprecated rendering modules - Move engine/render.py → engine/legacy/render.py (274 lines) - Move engine/layers.py → engine/legacy/layers.py (272 lines) - Update engine/legacy/layers.py to import from engine.legacy.render - Add comprehensive package documentation - Tests will be updated in next commit (Phase 3.3)
This commit is contained in:
15
engine/legacy/__init__.py
Normal file
15
engine/legacy/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
"""
|
||||
Legacy rendering modules for backwards compatibility.
|
||||
|
||||
This package contains deprecated rendering code from the old pipeline architecture.
|
||||
These modules are maintained for backwards compatibility with adapters and tests,
|
||||
but should not be used in new code.
|
||||
|
||||
New code should use the Stage-based pipeline architecture instead.
|
||||
|
||||
Modules:
|
||||
- render: Legacy font/gradient rendering functions
|
||||
- layers: Legacy layer compositing and effect application
|
||||
|
||||
All modules in this package are marked deprecated and will be removed in a future version.
|
||||
"""
|
||||
272
engine/legacy/layers.py
Normal file
272
engine/legacy/layers.py
Normal file
@@ -0,0 +1,272 @@
|
||||
"""
|
||||
Layer compositing — message overlay, ticker zone, firehose, noise.
|
||||
Depends on: config, render, effects.
|
||||
|
||||
.. deprecated::
|
||||
This module contains legacy rendering code. New pipeline code should
|
||||
use the Stage-based pipeline architecture instead. This module is
|
||||
maintained for backwards compatibility with the demo mode.
|
||||
"""
|
||||
|
||||
import random
|
||||
import re
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from engine import config
|
||||
from engine.effects import (
|
||||
EffectChain,
|
||||
EffectContext,
|
||||
fade_line,
|
||||
firehose_line,
|
||||
glitch_bar,
|
||||
noise,
|
||||
vis_offset,
|
||||
vis_trunc,
|
||||
)
|
||||
from engine.legacy.render import big_wrap, lr_gradient, lr_gradient_opposite
|
||||
from engine.terminal import RST, W_COOL
|
||||
|
||||
MSG_META = "\033[38;5;245m"
|
||||
MSG_BORDER = "\033[2;38;5;37m"
|
||||
|
||||
|
||||
def render_message_overlay(
|
||||
msg: tuple[str, str, float] | None,
|
||||
w: int,
|
||||
h: int,
|
||||
msg_cache: tuple,
|
||||
) -> tuple[list[str], tuple]:
|
||||
"""Render ntfy message overlay.
|
||||
|
||||
Args:
|
||||
msg: (title, body, timestamp) or None
|
||||
w: terminal width
|
||||
h: terminal height
|
||||
msg_cache: (cache_key, rendered_rows) for caching
|
||||
|
||||
Returns:
|
||||
(list of ANSI strings, updated cache)
|
||||
"""
|
||||
overlay = []
|
||||
if msg is None:
|
||||
return overlay, msg_cache
|
||||
|
||||
m_title, m_body, m_ts = msg
|
||||
display_text = m_body or m_title or "(empty)"
|
||||
display_text = re.sub(r"\s+", " ", display_text.upper())
|
||||
|
||||
cache_key = (display_text, w)
|
||||
if msg_cache[0] != cache_key:
|
||||
msg_rows = big_wrap(display_text, w - 4)
|
||||
msg_cache = (cache_key, msg_rows)
|
||||
else:
|
||||
msg_rows = msg_cache[1]
|
||||
|
||||
msg_rows = lr_gradient_opposite(
|
||||
msg_rows, (time.monotonic() * config.GRAD_SPEED) % 1.0
|
||||
)
|
||||
|
||||
elapsed_s = int(time.monotonic() - m_ts)
|
||||
remaining = max(0, config.MESSAGE_DISPLAY_SECS - elapsed_s)
|
||||
ts_str = datetime.now().strftime("%H:%M:%S")
|
||||
panel_h = len(msg_rows) + 2
|
||||
panel_top = max(0, (h - panel_h) // 2)
|
||||
|
||||
row_idx = 0
|
||||
for mr in msg_rows:
|
||||
ln = vis_trunc(mr, w)
|
||||
overlay.append(f"\033[{panel_top + row_idx + 1};1H {ln}\033[0m\033[K")
|
||||
row_idx += 1
|
||||
|
||||
meta_parts = []
|
||||
if m_title and m_title != m_body:
|
||||
meta_parts.append(m_title)
|
||||
meta_parts.append(f"ntfy \u00b7 {ts_str} \u00b7 {remaining}s")
|
||||
meta = (
|
||||
" " + " \u00b7 ".join(meta_parts)
|
||||
if len(meta_parts) > 1
|
||||
else " " + meta_parts[0]
|
||||
)
|
||||
overlay.append(f"\033[{panel_top + row_idx + 1};1H{MSG_META}{meta}\033[0m\033[K")
|
||||
row_idx += 1
|
||||
|
||||
bar = "\u2500" * (w - 4)
|
||||
overlay.append(f"\033[{panel_top + row_idx + 1};1H {MSG_BORDER}{bar}\033[0m\033[K")
|
||||
|
||||
return overlay, msg_cache
|
||||
|
||||
|
||||
def render_ticker_zone(
|
||||
active: list,
|
||||
scroll_cam: int,
|
||||
camera_x: int = 0,
|
||||
ticker_h: int = 0,
|
||||
w: int = 80,
|
||||
noise_cache: dict | None = None,
|
||||
grad_offset: float = 0.0,
|
||||
) -> tuple[list[str], dict]:
|
||||
"""Render the ticker scroll zone.
|
||||
|
||||
Args:
|
||||
active: list of (content_rows, color, canvas_y, meta_idx)
|
||||
scroll_cam: camera position (viewport top)
|
||||
camera_x: horizontal camera offset
|
||||
ticker_h: height of ticker zone
|
||||
w: terminal width
|
||||
noise_cache: dict of cy -> noise string
|
||||
grad_offset: gradient animation offset
|
||||
|
||||
Returns:
|
||||
(list of ANSI strings, updated noise_cache)
|
||||
"""
|
||||
if noise_cache is None:
|
||||
noise_cache = {}
|
||||
buf = []
|
||||
top_zone = max(1, int(ticker_h * 0.25))
|
||||
bot_zone = max(1, int(ticker_h * 0.10))
|
||||
|
||||
def noise_at(cy):
|
||||
if cy not in noise_cache:
|
||||
noise_cache[cy] = noise(w) if random.random() < 0.15 else None
|
||||
return noise_cache[cy]
|
||||
|
||||
for r in range(ticker_h):
|
||||
scr_row = r + 1
|
||||
cy = scroll_cam + r
|
||||
top_f = min(1.0, r / top_zone) if top_zone > 0 else 1.0
|
||||
bot_f = min(1.0, (ticker_h - 1 - r) / bot_zone) if bot_zone > 0 else 1.0
|
||||
row_fade = min(top_f, bot_f)
|
||||
drawn = False
|
||||
|
||||
for content, hc, by, midx in active:
|
||||
cr = cy - by
|
||||
if 0 <= cr < len(content):
|
||||
raw = content[cr]
|
||||
if cr != midx:
|
||||
colored = lr_gradient([raw], grad_offset)[0]
|
||||
else:
|
||||
colored = raw
|
||||
ln = vis_trunc(vis_offset(colored, camera_x), w)
|
||||
if row_fade < 1.0:
|
||||
ln = fade_line(ln, row_fade)
|
||||
|
||||
if cr == midx:
|
||||
buf.append(f"\033[{scr_row};1H{W_COOL}{ln}{RST}\033[K")
|
||||
elif ln.strip():
|
||||
buf.append(f"\033[{scr_row};1H{ln}{RST}\033[K")
|
||||
else:
|
||||
buf.append(f"\033[{scr_row};1H\033[K")
|
||||
drawn = True
|
||||
break
|
||||
|
||||
if not drawn:
|
||||
n = noise_at(cy)
|
||||
if row_fade < 1.0 and n:
|
||||
n = fade_line(n, row_fade)
|
||||
if n:
|
||||
buf.append(f"\033[{scr_row};1H{n}")
|
||||
else:
|
||||
buf.append(f"\033[{scr_row};1H\033[K")
|
||||
|
||||
return buf, noise_cache
|
||||
|
||||
|
||||
def apply_glitch(
|
||||
buf: list[str],
|
||||
ticker_buf_start: int,
|
||||
mic_excess: float,
|
||||
w: int,
|
||||
) -> list[str]:
|
||||
"""Apply glitch effect to ticker buffer.
|
||||
|
||||
Args:
|
||||
buf: current buffer
|
||||
ticker_buf_start: index where ticker starts in buffer
|
||||
mic_excess: mic level above threshold
|
||||
w: terminal width
|
||||
|
||||
Returns:
|
||||
Updated buffer with glitches applied
|
||||
"""
|
||||
glitch_prob = 0.32 + min(0.9, mic_excess * 0.16)
|
||||
n_hits = 4 + int(mic_excess / 2)
|
||||
ticker_buf_len = len(buf) - ticker_buf_start
|
||||
|
||||
if random.random() < glitch_prob and ticker_buf_len > 0:
|
||||
for _ in range(min(n_hits, ticker_buf_len)):
|
||||
gi = random.randint(0, ticker_buf_len - 1)
|
||||
scr_row = gi + 1
|
||||
buf[ticker_buf_start + gi] = f"\033[{scr_row};1H{glitch_bar(w)}"
|
||||
|
||||
return buf
|
||||
|
||||
|
||||
def render_firehose(items: list, w: int, fh: int, h: int) -> list[str]:
|
||||
"""Render firehose strip at bottom of screen."""
|
||||
buf = []
|
||||
if fh > 0:
|
||||
for fr in range(fh):
|
||||
scr_row = h - fh + fr + 1
|
||||
fline = firehose_line(items, w)
|
||||
buf.append(f"\033[{scr_row};1H{fline}\033[K")
|
||||
return buf
|
||||
|
||||
|
||||
_effect_chain = None
|
||||
|
||||
|
||||
def init_effects() -> None:
|
||||
"""Initialize effect plugins and chain."""
|
||||
global _effect_chain
|
||||
from engine.effects import EffectChain, get_registry
|
||||
|
||||
registry = get_registry()
|
||||
|
||||
import effects_plugins
|
||||
|
||||
effects_plugins.discover_plugins()
|
||||
|
||||
chain = EffectChain(registry)
|
||||
chain.set_order(["noise", "fade", "glitch", "firehose"])
|
||||
_effect_chain = chain
|
||||
|
||||
|
||||
def process_effects(
|
||||
buf: list[str],
|
||||
w: int,
|
||||
h: int,
|
||||
scroll_cam: int,
|
||||
ticker_h: int,
|
||||
camera_x: int = 0,
|
||||
mic_excess: float = 0.0,
|
||||
grad_offset: float = 0.0,
|
||||
frame_number: int = 0,
|
||||
has_message: bool = False,
|
||||
items: list | None = None,
|
||||
) -> list[str]:
|
||||
"""Process buffer through effect chain."""
|
||||
if _effect_chain is None:
|
||||
init_effects()
|
||||
|
||||
ctx = EffectContext(
|
||||
terminal_width=w,
|
||||
terminal_height=h,
|
||||
scroll_cam=scroll_cam,
|
||||
camera_x=camera_x,
|
||||
ticker_height=ticker_h,
|
||||
mic_excess=mic_excess,
|
||||
grad_offset=grad_offset,
|
||||
frame_number=frame_number,
|
||||
has_message=has_message,
|
||||
items=items or [],
|
||||
)
|
||||
return _effect_chain.process(buf, ctx)
|
||||
|
||||
|
||||
def get_effect_chain() -> EffectChain | None:
|
||||
"""Get the effect chain instance."""
|
||||
global _effect_chain
|
||||
if _effect_chain is None:
|
||||
init_effects()
|
||||
return _effect_chain
|
||||
274
engine/legacy/render.py
Normal file
274
engine/legacy/render.py
Normal file
@@ -0,0 +1,274 @@
|
||||
"""
|
||||
OTF → terminal half-block rendering pipeline.
|
||||
Font loading, text rasterization, word-wrap, gradient coloring, headline block assembly.
|
||||
Depends on: config, terminal, sources, translate.
|
||||
|
||||
.. deprecated::
|
||||
This module contains legacy rendering code. New pipeline code should
|
||||
use the Stage-based pipeline architecture instead. This module is
|
||||
maintained for backwards compatibility with the demo mode.
|
||||
"""
|
||||
|
||||
import random
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from engine import config
|
||||
from engine.sources import NO_UPPER, SCRIPT_FONTS, SOURCE_LANGS
|
||||
from engine.terminal import RST
|
||||
from engine.translate import detect_location_language, translate_headline
|
||||
|
||||
# ─── GRADIENT ─────────────────────────────────────────────
|
||||
# Left → right: white-hot leading edge fades to near-black
|
||||
GRAD_COLS = [
|
||||
"\033[1;38;5;231m", # white
|
||||
"\033[1;38;5;195m", # pale cyan-white
|
||||
"\033[38;5;123m", # bright cyan
|
||||
"\033[38;5;118m", # bright lime
|
||||
"\033[38;5;82m", # lime
|
||||
"\033[38;5;46m", # bright green
|
||||
"\033[38;5;40m", # green
|
||||
"\033[38;5;34m", # medium green
|
||||
"\033[38;5;28m", # dark green
|
||||
"\033[38;5;22m", # deep green
|
||||
"\033[2;38;5;22m", # dim deep green
|
||||
"\033[2;38;5;235m", # near black
|
||||
]
|
||||
|
||||
# Complementary sweep for queue messages (opposite hue family from ticker greens)
|
||||
MSG_GRAD_COLS = [
|
||||
"\033[1;38;5;231m", # white
|
||||
"\033[1;38;5;225m", # pale pink-white
|
||||
"\033[38;5;219m", # bright pink
|
||||
"\033[38;5;213m", # hot pink
|
||||
"\033[38;5;207m", # magenta
|
||||
"\033[38;5;201m", # bright magenta
|
||||
"\033[38;5;165m", # orchid-red
|
||||
"\033[38;5;161m", # ruby-magenta
|
||||
"\033[38;5;125m", # dark magenta
|
||||
"\033[38;5;89m", # deep maroon-magenta
|
||||
"\033[2;38;5;89m", # dim deep maroon-magenta
|
||||
"\033[2;38;5;235m", # near black
|
||||
]
|
||||
|
||||
# ─── FONT LOADING ─────────────────────────────────────────
|
||||
_FONT_OBJ = None
|
||||
_FONT_OBJ_KEY = None
|
||||
_FONT_CACHE = {}
|
||||
|
||||
|
||||
def font():
|
||||
"""Lazy-load the primary OTF font (path + face index aware)."""
|
||||
global _FONT_OBJ, _FONT_OBJ_KEY
|
||||
if not config.FONT_PATH:
|
||||
raise FileNotFoundError(
|
||||
f"No primary font selected. Add .otf/.ttf/.ttc files to {config.FONT_DIR}."
|
||||
)
|
||||
key = (config.FONT_PATH, config.FONT_INDEX, config.FONT_SZ)
|
||||
if _FONT_OBJ is None or key != _FONT_OBJ_KEY:
|
||||
_FONT_OBJ = ImageFont.truetype(
|
||||
config.FONT_PATH, config.FONT_SZ, index=config.FONT_INDEX
|
||||
)
|
||||
_FONT_OBJ_KEY = key
|
||||
return _FONT_OBJ
|
||||
|
||||
|
||||
def clear_font_cache():
|
||||
"""Reset cached font objects after changing primary font selection."""
|
||||
global _FONT_OBJ, _FONT_OBJ_KEY
|
||||
_FONT_OBJ = None
|
||||
_FONT_OBJ_KEY = None
|
||||
|
||||
|
||||
def load_font_face(font_path, font_index=0, size=None):
|
||||
"""Load a specific face from a font file or collection."""
|
||||
font_size = size or config.FONT_SZ
|
||||
return ImageFont.truetype(font_path, font_size, index=font_index)
|
||||
|
||||
|
||||
def list_font_faces(font_path, max_faces=64):
|
||||
"""Return discoverable face indexes + display names from a font file."""
|
||||
faces = []
|
||||
for idx in range(max_faces):
|
||||
try:
|
||||
fnt = load_font_face(font_path, idx)
|
||||
except Exception:
|
||||
if idx == 0:
|
||||
raise
|
||||
break
|
||||
family, style = fnt.getname()
|
||||
display = f"{family} {style}".strip()
|
||||
if not display:
|
||||
display = f"{Path(font_path).stem} [{idx}]"
|
||||
faces.append({"index": idx, "name": display})
|
||||
return faces
|
||||
|
||||
|
||||
def font_for_lang(lang=None):
|
||||
"""Get appropriate font for a language."""
|
||||
if lang is None or lang not in SCRIPT_FONTS:
|
||||
return font()
|
||||
if lang not in _FONT_CACHE:
|
||||
try:
|
||||
_FONT_CACHE[lang] = ImageFont.truetype(SCRIPT_FONTS[lang], config.FONT_SZ)
|
||||
except Exception:
|
||||
_FONT_CACHE[lang] = font()
|
||||
return _FONT_CACHE[lang]
|
||||
|
||||
|
||||
# ─── RASTERIZATION ────────────────────────────────────────
|
||||
def render_line(text, fnt=None):
|
||||
"""Render a line of text as terminal rows using OTF font + half-blocks."""
|
||||
if fnt is None:
|
||||
fnt = font()
|
||||
bbox = fnt.getbbox(text)
|
||||
if not bbox or bbox[2] <= bbox[0]:
|
||||
return [""]
|
||||
pad = 4
|
||||
img_w = bbox[2] - bbox[0] + pad * 2
|
||||
img_h = bbox[3] - bbox[1] + pad * 2
|
||||
img = Image.new("L", (img_w, img_h), 0)
|
||||
draw = ImageDraw.Draw(img)
|
||||
draw.text((-bbox[0] + pad, -bbox[1] + pad), text, fill=255, font=fnt)
|
||||
pix_h = config.RENDER_H * 2
|
||||
hi_h = pix_h * config.SSAA
|
||||
scale = hi_h / max(img_h, 1)
|
||||
new_w_hi = max(1, int(img_w * scale))
|
||||
img = img.resize((new_w_hi, hi_h), Image.Resampling.LANCZOS)
|
||||
new_w = max(1, int(new_w_hi / config.SSAA))
|
||||
img = img.resize((new_w, pix_h), Image.Resampling.LANCZOS)
|
||||
data = img.tobytes()
|
||||
thr = 80
|
||||
rows = []
|
||||
for y in range(0, pix_h, 2):
|
||||
row = []
|
||||
for x in range(new_w):
|
||||
top = data[y * new_w + x] > thr
|
||||
bot = data[(y + 1) * new_w + x] > thr if y + 1 < pix_h else False
|
||||
if top and bot:
|
||||
row.append("█")
|
||||
elif top:
|
||||
row.append("▀")
|
||||
elif bot:
|
||||
row.append("▄")
|
||||
else:
|
||||
row.append(" ")
|
||||
rows.append("".join(row))
|
||||
while rows and not rows[-1].strip():
|
||||
rows.pop()
|
||||
while rows and not rows[0].strip():
|
||||
rows.pop(0)
|
||||
return rows if rows else [""]
|
||||
|
||||
|
||||
def big_wrap(text, max_w, fnt=None):
|
||||
"""Word-wrap text and render with OTF font."""
|
||||
if fnt is None:
|
||||
fnt = font()
|
||||
words = text.split()
|
||||
lines, cur = [], ""
|
||||
for word in words:
|
||||
test = f"{cur} {word}".strip() if cur else word
|
||||
bbox = fnt.getbbox(test)
|
||||
if bbox:
|
||||
img_h = bbox[3] - bbox[1] + 8
|
||||
pix_h = config.RENDER_H * 2
|
||||
scale = pix_h / max(img_h, 1)
|
||||
term_w = int((bbox[2] - bbox[0] + 8) * scale)
|
||||
else:
|
||||
term_w = 0
|
||||
if term_w > max_w - 4 and cur:
|
||||
lines.append(cur)
|
||||
cur = word
|
||||
else:
|
||||
cur = test
|
||||
if cur:
|
||||
lines.append(cur)
|
||||
out = []
|
||||
for i, ln in enumerate(lines):
|
||||
out.extend(render_line(ln, fnt))
|
||||
if i < len(lines) - 1:
|
||||
out.append("")
|
||||
return out
|
||||
|
||||
|
||||
def lr_gradient(rows, offset=0.0, grad_cols=None):
|
||||
"""Color each non-space block character with a shifting left-to-right gradient."""
|
||||
cols = grad_cols or GRAD_COLS
|
||||
n = len(cols)
|
||||
max_x = max((len(r.rstrip()) for r in rows if r.strip()), default=1)
|
||||
out = []
|
||||
for row in rows:
|
||||
if not row.strip():
|
||||
out.append(row)
|
||||
continue
|
||||
buf = []
|
||||
for x, ch in enumerate(row):
|
||||
if ch == " ":
|
||||
buf.append(" ")
|
||||
else:
|
||||
shifted = (x / max(max_x - 1, 1) + offset) % 1.0
|
||||
idx = min(round(shifted * (n - 1)), n - 1)
|
||||
buf.append(f"{cols[idx]}{ch}{RST}")
|
||||
out.append("".join(buf))
|
||||
return out
|
||||
|
||||
|
||||
def lr_gradient_opposite(rows, offset=0.0):
|
||||
"""Complementary (opposite wheel) gradient used for queue message panels."""
|
||||
return lr_gradient(rows, offset, MSG_GRAD_COLS)
|
||||
|
||||
|
||||
# ─── HEADLINE BLOCK ASSEMBLY ─────────────────────────────
|
||||
def make_block(title, src, ts, w):
|
||||
"""Render a headline into a content block with color."""
|
||||
target_lang = (
|
||||
(SOURCE_LANGS.get(src) or detect_location_language(title))
|
||||
if config.MODE == "news"
|
||||
else None
|
||||
)
|
||||
lang_font = font_for_lang(target_lang)
|
||||
if target_lang:
|
||||
title = translate_headline(title, target_lang)
|
||||
# Don't uppercase scripts that have no case (CJK, Arabic, etc.)
|
||||
if target_lang and target_lang in NO_UPPER:
|
||||
title_up = re.sub(r"\s+", " ", title)
|
||||
else:
|
||||
title_up = re.sub(r"\s+", " ", title.upper())
|
||||
for old, new in [
|
||||
("\u2019", "'"),
|
||||
("\u2018", "'"),
|
||||
("\u201c", '"'),
|
||||
("\u201d", '"'),
|
||||
("\u2013", "-"),
|
||||
("\u2014", "-"),
|
||||
]:
|
||||
title_up = title_up.replace(old, new)
|
||||
big_rows = big_wrap(title_up, w - 4, lang_font)
|
||||
hc = random.choice(
|
||||
[
|
||||
"\033[38;5;46m", # matrix green
|
||||
"\033[38;5;34m", # dark green
|
||||
"\033[38;5;82m", # lime
|
||||
"\033[38;5;48m", # sea green
|
||||
"\033[38;5;37m", # teal
|
||||
"\033[38;5;44m", # cyan
|
||||
"\033[38;5;87m", # sky
|
||||
"\033[38;5;117m", # ice blue
|
||||
"\033[38;5;250m", # cool white
|
||||
"\033[38;5;156m", # pale green
|
||||
"\033[38;5;120m", # mint
|
||||
"\033[38;5;80m", # dark cyan
|
||||
"\033[38;5;108m", # grey-green
|
||||
"\033[38;5;115m", # sage
|
||||
"\033[1;38;5;46m", # bold green
|
||||
"\033[1;38;5;250m", # bold white
|
||||
]
|
||||
)
|
||||
content = [" " + r for r in big_rows]
|
||||
content.append("")
|
||||
meta = f"\u2591 {src} \u00b7 {ts}"
|
||||
content.append(" " * max(2, w - len(meta) - 2) + meta)
|
||||
return content, hc, len(content) - 1 # (rows, color, meta_row_index)
|
||||
Reference in New Issue
Block a user