Nice cards templates
CI / quality (3.11) (push) Has been cancelled
CI / quality (3.12) (push) Has been cancelled

This commit is contained in:
2026-10-16 02:37:28 +02:00
parent 3b886acaba
commit 89855f78f3
11 changed files with 564 additions and 304 deletions
+6 -1
View File
@@ -23,6 +23,11 @@ The pipeline is deliberately split into three inspectable stages.
- **Modular rendering** – each card is written as a *self-contained* `.html` file
(CSS inlined, images referenced relatively) so a badly laid-out card can be
tweaked by hand and re-rasterised without re-running the earlier stages.
- **Official-style card faces** – templates reproduce the printed TCG look: a gold
frame with corner stars, HP teardrop, engraved name plaque, the
element / faction / weapon tag column, hexagonal cost dice and a frosted
description panel. Keyword colours are re-mapped at render time so the
dataset's pastel palette stays legible on the light panel.
- **Print-ready output** – cards are rendered at the classic 63x88 mm poker
format at 300 DPI (configurable) and imposed 3x3 on A4 with optional crop marks.
- **Wiki-derived factions** – `genshin-tcg factions` resolves each character's
@@ -160,7 +165,7 @@ src/genshin_tcg/
├── pipeline.py # fetch / render / impose orchestration
├── data/ # remote source, catalogue, asset download, wiki factions
├── rendering/ # Jinja environment, view models, converter, renderer
│ └── templates/ # base/character/action templates + stylesheet
│ └── templates/ # base/character/action templates + macros + stylesheet
└── pdf/ # ReportLab imposition
```
+67 -11
View File
@@ -13,12 +13,13 @@ small allow-list of tags/styles, making the output safe to inject with Jinja's
from __future__ import annotations
import colorsys
import re
from collections.abc import Mapping
from collections.abc import Callable, Mapping
from bs4 import BeautifulSoup, Tag
__all__ = ["clean_markup", "inline_symbols", "sanitize", "strip_markup"]
__all__ = ["clean_markup", "inline_symbols", "panel_ink", "sanitize", "strip_markup"]
# ``<color=#RRGGBB>`` / ``<color=#RRGGBBAA>`` -> span with an inline style.
_COLOR_OPEN = re.compile(r"<color=(#[0-9A-Fa-f]{6}(?:[0-9A-Fa-f]{2})?)>", re.IGNORECASE)
@@ -51,7 +52,7 @@ _DROP_CONTENT_TAGS: tuple[str, ...] = (
)
def _clean_style(style: str) -> str:
def _clean_style(style: str, color_map: Callable[[str], str] | None = None) -> str:
"""Keep only the allow-listed declarations of an inline ``style`` attribute."""
declarations: list[str] = []
for raw in style.split(";"):
@@ -61,7 +62,10 @@ def _clean_style(style: str) -> str:
prop, _, value = declaration.partition(":")
prop = prop.strip().lower()
value = value.strip()
if prop in _ALLOWED_STYLE_PROPS and value:
if prop not in _ALLOWED_STYLE_PROPS or not value:
continue
if prop == "color" and color_map is not None:
value = color_map(value)
declarations.append(f"{prop}: {value}")
return "; ".join(declarations)
@@ -74,7 +78,9 @@ def _prepare(text: str) -> str:
return text
def sanitize(fragment: str) -> str:
def sanitize(
fragment: str, *, color_map: Callable[[str], str] | None = None
) -> str:
"""Return ``fragment`` restricted to the allow-listed subset of HTML."""
soup = BeautifulSoup(fragment, "html.parser")
for dangerous in soup.find_all(list(_DROP_CONTENT_TAGS)):
@@ -84,27 +90,77 @@ def sanitize(fragment: str) -> str:
if allowed_attrs is None:
tag.unwrap()
continue
_filter_attributes(tag, allowed_attrs)
_filter_attributes(tag, allowed_attrs, color_map)
return "".join(str(child) for child in soup.children)
def _filter_attributes(tag: Tag, allowed_attrs: frozenset[str]) -> None:
def _filter_attributes(
tag: Tag,
allowed_attrs: frozenset[str],
color_map: Callable[[str], str] | None = None,
) -> None:
for name in list(tag.attrs):
if name not in allowed_attrs:
del tag.attrs[name]
style = tag.get("style")
if isinstance(style, str):
cleaned = _clean_style(style)
cleaned = _clean_style(style, color_map)
if cleaned:
tag["style"] = cleaned
else:
del tag["style"]
def clean_markup(text: str) -> str:
"""Full pipeline: colour tags -> ``<br/>`` -> sanitised HTML string."""
return sanitize(_prepare(text))
def clean_markup(
text: str, *, color_map: Callable[[str], str] | None = None
) -> str:
"""Full pipeline: colour tags -> ``<br/>`` -> sanitised HTML string.
``color_map`` optionally rewrites each inline colour (see :func:`panel_ink`)
so the output suits the destination background.
"""
return sanitize(_prepare(text), color_map=color_map)
#: Neutral ink substituted for achromatic (white/grey) keyword colours.
_PANEL_INK = "#3a332a"
#: Lightness forced onto chromatic keyword colours so they read on a light panel.
_PANEL_LIGHTNESS = 0.34
#: Saturation ceiling: the source pastels are fully saturated and would look
#: garish once darkened.
_PANEL_MAX_SATURATION = 0.75
#: Below this saturation a colour counts as achromatic.
_PANEL_MIN_SATURATION = 0.18
_HEX_COLOR = re.compile(r"^#?([0-9A-Fa-f]{6})(?:[0-9A-Fa-f]{2})?$")
def _parse_hex_color(value: str) -> tuple[int, int, int] | None:
match = _HEX_COLOR.match(value.strip())
if match is None:
return None
digits = match.group(1)
return int(digits[0:2], 16), int(digits[2:4], 16), int(digits[4:6], 16)
def panel_ink(color: str) -> str:
"""Darken an upstream keyword colour so it stays legible on the light panel.
The dataset colours its keywords for the official card's darker text box
(mostly plain white), which would be invisible on the frosted-white panel
used here. Achromatic colours collapse to a neutral ink; chromatic ones keep
their hue but are darkened enough for contrast.
"""
rgb = _parse_hex_color(color)
if rgb is None:
return color
hue, _, saturation = colorsys.rgb_to_hls(*(channel / 255 for channel in rgb))
if saturation < _PANEL_MIN_SATURATION:
return _PANEL_INK
red, green, blue = colorsys.hls_to_rgb(
hue, _PANEL_LIGHTNESS, min(_PANEL_MAX_SATURATION, saturation)
)
return f"#{round(red * 255):02X}{round(green * 255):02X}{round(blue * 255):02X}"
def strip_markup(text: str) -> str:
+25 -9
View File
@@ -17,7 +17,7 @@ from pydantic import BaseModel, ConfigDict
from genshin_tcg.constants import AssetPaths, CostType, Element
from genshin_tcg.logging_setup import get_logger
from genshin_tcg.markup import clean_markup, inline_symbols
from genshin_tcg.markup import clean_markup, inline_symbols, panel_ink
from genshin_tcg.models import ActionCard, Card, CharacterCard, CostItem
__all__ = [
@@ -35,12 +35,16 @@ __all__ = [
_logger = get_logger("rendering.context")
#: Fallback labels so the pipeline works offline / in tests.
#:
#: ``WeaponType.NONE`` and ``CardType.NONE`` share the ``"none"`` token but mean
#: different things ("other weapon" vs "event card"), so weapon lookups go through
#: the qualified ``weapon.<token>`` key (see :meth:`_ViewBuilder._weapon_label`).
_DEFAULT_LABELS: dict[str, str] = {
"character": "Personnage",
"talent": "Aptitude",
"artifact": "Artéfact",
"weapon": "Arme",
"none": "Sans élément",
"none": "Événement",
"resonance": "Résonance élémentaire",
"ally": "Compagnon",
"food": "Plat",
@@ -58,6 +62,7 @@ _DEFAULT_LABELS: dict[str, str] = {
"pole": "Arme d'hast",
"bow": "Arc",
"catalyst": "Catalyseur",
"weapon.none": "Autre arme",
"void": "Dé indéterminé",
"energy": "Énergie",
"same": "Dé identique",
@@ -228,7 +233,7 @@ class _ViewBuilder:
# -- Internals -------------------------------------------------------------
def _description(self, raw: str) -> str:
html = clean_markup(raw)
html = clean_markup(raw, color_map=panel_ink)
if not self._inline:
return html
symbols = {
@@ -259,18 +264,22 @@ class _ViewBuilder:
for item in items
)
def _weapon_label(self, weapon: str) -> str:
"""Localised weapon name.
``WeaponType.NONE`` and ``CardType.NONE`` share the ``"none"`` token, so
weapons are looked up under the qualified ``weapon.<token>`` key first.
"""
return self._labels.get(f"weapon.{weapon}") or self._labels.get(weapon, weapon)
def _character(self, card: CharacterCard, *, width_px: int, height_px: int) -> CharacterView:
# Tag order mirrors the official print: element, nation/faction, weapon.
tags: list[TagView] = [
TagView(
kind="element",
label=self._labels.get(card.element.value, card.element.value),
icon=self._resolver.url(AssetPaths.element(card.element)),
),
TagView(
kind="weapon",
label=self._labels.get(card.weapon.value, card.weapon.value),
icon=self._resolver.url(AssetPaths.weapon(card.weapon)),
),
]
faction = self._factions.get(card.id)
if faction:
@@ -286,6 +295,13 @@ class _ViewBuilder:
),
)
)
tags.append(
TagView(
kind="weapon",
label=self._weapon_label(card.weapon.value),
icon=self._resolver.url(AssetPaths.weapon(card.weapon)),
)
)
skills = tuple(
SkillView(
@@ -316,7 +332,7 @@ class _ViewBuilder:
element_icon=self._resolver.url(AssetPaths.element(card.element)),
element_label=self._labels.get(card.element.value, card.element.value),
weapon_icon=self._resolver.url(AssetPaths.weapon(card.weapon)),
weapon_label=self._labels.get(card.weapon.value, card.weapon.value),
weapon_label=self._weapon_label(card.weapon.value),
tags=tuple(tags),
skills=skills,
)
-11
View File
@@ -29,19 +29,11 @@ from genshin_tcg.rendering.converters import HtmlConverter
from genshin_tcg.rendering.environment import build_environment
__all__ = [
"ACTION_HEADER_RATIO",
"BODY_RATIO",
"CHARACTER_HEADER_RATIO",
"CardRenderer",
"RenderedCard",
"safe_name",
]
#: Vertical zoning (see the design document, section 4).
CHARACTER_HEADER_RATIO = 0.20
ACTION_HEADER_RATIO = 0.15
BODY_RATIO = 0.40
_PROGRESS_EVERY = 25
@@ -150,9 +142,6 @@ class CardRenderer:
return template.render(
width_px=card.design_width_px,
height_px=card.design_height_px,
header_height_px=round(card.design_height_px * CHARACTER_HEADER_RATIO),
header_action_height_px=round(card.design_height_px * ACTION_HEADER_RATIO),
body_height_px=round(card.design_height_px * BODY_RATIO),
)
@@ -1,26 +1,23 @@
{% extends "base.html.jinja" %}
{% import "macros.html.jinja" as ui %}
{% block card %}
<div class="art-bg" style="background-image: url('{{ view.illustration }}')"></div>
<img class="art art-contain" src="{{ view.illustration }}" alt="">
<div class="shade"></div>
<header class="header header-action">
<div class="cost cost-header">
{% for die in view.cost %}
<span class="die" style="background-image: url('{{ die.icon }}')">
<b>{{ die.count }}</b>
</span>
{% endfor %}
<div class="top-bar">
<div class="cost-hero">
{%- for die in view.cost %}
<span class="die" style="background-image: url('{{ die.icon }}')"><b>{{ die.count }}</b></span>
{%- endfor %}
</div>
<h1 class="card-name">{{ view.name }}</h1>
</header>
<section class="body body-action">
<div class="type-row">
<img class="type-icon" src="{{ view.type_icon }}" alt="">
<span class="type-label">{{ view.type_label }}</span>
{{ ui.plaque(view.name) }}
<div class="type-badge">
<img src="{{ view.type_icon }}" alt="">
<span>{{ view.type_label }}</span>
</div>
</div>
<section class="panel">
<p class="desc">{{ view.description_html | safe }}</p>
{% if view.requirement %}
<p class="requirement">Requiert : {{ view.requirement }}</p>
@@ -7,7 +7,14 @@
<style>{{ style_content | safe }}</style>
</head>
<body>
{%- macro corner_star(position) -%}
<svg class="star star-{{ position }}" viewBox="0 0 24 24" aria-hidden="true"><path d="M23 1C21.75 5.67 2.25 5.67 1 1 5.67 2.25 5.67 21.75 1 23 2.25 18.33 21.75 18.33 23 23 18.33 21.75 18.33 2.25 23 1Z"/></svg>
{%- endmacro -%}
<div id="card" class="card {{ card_class }}">
<img class="art" src="{{ view.illustration }}" alt="">
<div class="art-scrim" aria-hidden="true"></div>
<div class="frame-line" aria-hidden="true"></div>
{{ corner_star("tl") }}{{ corner_star("tr") }}{{ corner_star("bl") }}{{ corner_star("br") }}
{% block card %}{% endblock %}
</div>
</body>
+317 -193
View File
@@ -1,26 +1,41 @@
{# Self-contained stylesheet for a single card. Dimensions are injected so the
generated HTML can be re-rendered at any DPI without editing the template. #}
{# Self-contained stylesheet for a single card, styled after the official
Genshin TCG print (gold/cream frame, HP teardrop, name plaque, right-hand
element/nation/weapon tags, frosted text panel). Dimensions are injected so
the card can be re-rendered at any DPI without editing the template.
1rem == 10px at the 744x1039 design size, so every dimension below scales
with the configured card width. #}
:root {
--card-w: {{ width_px }}px;
--card-h: {{ height_px }}px;
--header-h: {{ header_height_px }}px;
--header-action-h: {{ header_action_height_px }}px;
--body-h: {{ body_height_px }}px;
--accent: #d9c9a3;
--gold-0: #fffaf0;
--gold-1: #f7ead0;
--gold-2: #ecdcb6;
--gold-3: #ddc48f;
--gold-4: #c8a45f;
--gold-line: #c9a24e;
--gold-deep: #7c5f2a;
--ink: #3a332a;
--ink-soft: #6a5b41;
--panel-bg: rgba(252, 250, 244, 0.45);
}
html { font-size: calc(var(--card-w) / 74.4); }
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
width: var(--card-w);
height: var(--card-h);
overflow: hidden;
background: #0b0f18;
background: #fff;
}
body {
font-family: "Segoe UI", "Noto Sans", "DejaVu Sans", system-ui, sans-serif;
-webkit-font-smoothing: antialiased;
color: var(--ink);
}
.card {
@@ -28,238 +43,347 @@ body {
width: var(--card-w);
height: var(--card-h);
overflow: hidden;
color: #f4f1e8;
background: radial-gradient(120% 80% at 50% 0%, #26304a 0%, #0d1220 70%);
border-radius: 20px;
box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.12);
border-radius: 3.4rem;
background: linear-gradient(152deg, var(--gold-0) 0%, var(--gold-1) 30%,
var(--gold-2) 62%, var(--gold-3) 100%);
}
/* Element accents ------------------------------------------------------------ */
.el-anemo { --accent: #7fe0c3; }
.el-geo { --accent: #f0b23a; }
.el-electro { --accent: #c9a6ff; }
.el-dendro { --accent: #9fdc5b; }
.el-hydro { --accent: #6cc6ff; }
.el-pyro { --accent: #ff8a5c; }
.el-cryo { --accent: #9fd8e8; }
/* Artwork -------------------------------------------------------------------- */
.art {
position: absolute;
inset: 0 0 auto 0;
width: 100%;
height: 60%;
left: 2.6rem;
top: 2.6rem;
width: calc(100% - 5.2rem);
height: calc(100% - 5.2rem);
object-fit: cover;
object-position: top center;
object-position: center;
border-radius: 1.8rem;
}
/* Action cards keep the whole illustration visible, over a blurred backdrop. */
.art-bg {
.art-scrim {
position: absolute;
inset: 0 0 auto 0;
height: 60%;
background-size: cover;
background-position: center;
filter: blur(26px) brightness(0.5) saturate(0.9);
transform: scale(1.15);
}
.art-contain { object-fit: contain; object-position: center; }
.shade {
position: absolute;
inset: 0 0 auto 0;
height: 60%;
left: 2.6rem;
top: 2.6rem;
width: calc(100% - 5.2rem);
height: calc(100% - 5.2rem);
border-radius: 1.8rem;
pointer-events: none;
background: linear-gradient(
180deg,
rgba(8, 10, 18, 0.86) 0%,
rgba(8, 10, 18, 0.2) 26%,
rgba(8, 10, 18, 0) 55%,
rgba(8, 10, 18, 0.65) 100%
rgba(26, 18, 6, 0.34) 0%,
rgba(26, 18, 6, 0.1) 10%,
rgba(26, 18, 6, 0) 24%,
rgba(26, 18, 6, 0) 58%,
rgba(26, 18, 6, 0.2) 80%,
rgba(26, 18, 6, 0.42) 100%
);
}
/* Header --------------------------------------------------------------------- */
.header {
.frame-line {
position: absolute;
inset: 0 0 auto 0;
height: var(--header-h);
display: flex;
align-items: flex-start;
gap: 14px;
padding: 20px 22px 0;
left: 2.6rem;
top: 2.6rem;
width: calc(100% - 5.2rem);
height: calc(100% - 5.2rem);
border-radius: 1.8rem;
border: 0.2rem solid rgba(201, 162, 78, 0.72);
pointer-events: none;
}
.hp-bubble {
position: relative;
flex: 0 0 auto;
width: 96px;
height: 96px;
.star {
position: absolute;
width: 2.2rem;
height: 2.2rem;
fill: var(--gold-4);
opacity: 0.92;
}
.star-tl { left: 0.9rem; top: 0.9rem; }
.star-tr { right: 0.9rem; top: 0.9rem; }
.star-bl { left: 0.9rem; bottom: 0.9rem; }
.star-br { right: 0.9rem; bottom: 0.9rem; }
/* Name plaque (shared) ------------------------------------------------------- */
.plaque {
position: absolute;
top: 3.4rem;
left: 0;
right: 0;
width: max-content;
min-width: 32rem;
max-width: 46rem;
margin: 0 auto;
min-height: 5.4rem;
display: flex;
align-items: center;
justify-content: center;
padding: 0.5rem 4.4rem;
border-radius: 1.4rem;
background: linear-gradient(180deg, #fefbef 0%, #f6ebce 46%, #e6d0a2 100%);
border: 0.2rem solid var(--gold-line);
box-shadow: inset 0 0 0 0.2rem rgba(255, 255, 255, 0.5),
0 0.2rem 0.6rem rgba(70, 50, 15, 0.35);
z-index: 3;
}
/* Long names get a tighter paddings/font so they still fit on one or two lines. */
.plaque--long { padding-left: 3.4rem; padding-right: 3.4rem; }
.plaque--long .plaque-name { font-size: 2.4rem; }
.plaque--xlong .plaque-name { font-size: 2.05rem; }
.plaque::before,
.plaque::after {
content: "";
position: absolute;
top: 50%;
width: 1.1rem;
height: 1.1rem;
background: var(--gold-4);
border-radius: 0.1rem;
transform: translateY(-50%) rotate(45deg);
box-shadow: inset 0 0 0 0.15rem rgba(255, 250, 235, 0.55);
}
.plaque::before { left: 1.4rem; }
.plaque::after { right: 1.4rem; }
.plaque-name {
font-size: 2.95rem;
font-weight: 800;
line-height: 1.12;
letter-spacing: 0.02rem;
color: #6a4d16;
text-align: center;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
/* Character: HP + attribute tags -------------------------------------------- */
.hp {
position: absolute;
left: 0.9rem;
top: 0.4rem;
width: 14.2rem;
height: 19.4rem;
z-index: 3;
}
.hp img {
width: 100%;
height: 100%;
object-fit: contain;
filter: drop-shadow(0 0.2rem 0.3rem rgba(40, 25, 5, 0.45));
}
.hp b {
position: absolute;
inset: 1.6rem 0 0 0;
display: grid;
place-items: center;
}
.hp-bubble img { width: 100%; height: 100%; }
.hp-bubble b {
position: absolute;
font-size: 40px;
font-size: 8.6rem;
font-weight: 800;
color: #fff;
-webkit-text-stroke: 3px #1a1a1a;
-webkit-text-stroke: 0.42rem var(--gold-deep);
paint-order: stroke fill;
}
.title-block { flex: 1 1 auto; min-width: 0; padding-top: 6px; }
.card-name {
font-size: 42px;
line-height: 1.02;
font-weight: 800;
letter-spacing: 0.4px;
text-shadow: 0 2px 6px rgba(0, 0, 0, 0.85);
}
.header-tags {
list-style: none;
.tags {
position: absolute;
top: 5.8rem;
right: 3.2rem;
width: 8.2rem;
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 8px;
flex-direction: column;
align-items: center;
gap: 1.3rem;
z-index: 3;
}
.header-tags li {
font-size: 17px;
font-weight: 600;
letter-spacing: 0.3px;
padding: 3px 12px;
border-radius: 999px;
color: var(--accent);
background: rgba(0, 0, 0, 0.45);
border: 1px solid color-mix(in srgb, var(--accent) 55%, transparent);
.tag {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.15rem;
width: 100%;
}
/* Cost dice (action header + skills) ---------------------------------------- */
.tag-icon {
width: 5.2rem;
height: 5.2rem;
object-fit: contain;
filter: drop-shadow(0 0.1rem 0.25rem rgba(0, 0, 0, 0.7));
}
.tag-label {
font-size: 1.35rem;
font-weight: 700;
line-height: 1.08;
text-align: center;
color: #fff;
text-shadow: 0 0.1rem 0.2rem rgba(0, 0, 0, 0.9), 0 0 0.35rem rgba(0, 0, 0, 0.75);
}
.tag-fallback {
display: grid;
place-items: center;
width: 4.8rem;
height: 4.8rem;
border-radius: 50%;
font-size: 1.1rem;
font-weight: 700;
line-height: 1.05;
text-align: center;
color: #fff;
background: rgba(35, 28, 16, 0.62);
box-shadow: inset 0 0 0 0.15rem rgba(255, 240, 200, 0.5);
}
/* Frosted text panel (shared) ------------------------------------------------ */
.panel {
position: absolute;
left: 4.4rem;
right: 4.4rem;
bottom: 4.6rem;
padding: 1.5rem 1.8rem 1.6rem;
border-radius: 1.7rem;
background: var(--panel-bg);
-webkit-backdrop-filter: blur(0.5rem);
backdrop-filter: blur(0.5rem);
box-shadow: inset 0 0 0 0.12rem rgba(255, 255, 255, 0.7),
0 0.4rem 1.3rem rgba(12, 12, 22, 0.28);
color: var(--ink);
z-index: 4;
}
/* Skills --------------------------------------------------------------------- */
/* One grid for the whole list, so every row shares the same dice-column width
and the descriptions all start on the same x. */
.skills {
display: grid;
grid-template-columns: auto minmax(0, 1fr);
column-gap: 0.8rem;
row-gap: 0.85rem;
align-items: start;
}
.dice { display: flex; gap: 0.25rem; padding-top: 0.15rem; }
.die {
position: relative;
display: inline-grid;
display: grid;
place-items: center;
width: 38px;
height: 38px;
width: 2.6rem;
height: 2.9rem;
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
.die b {
font-size: 19px;
font-size: 1.45rem;
font-weight: 800;
color: #fff;
-webkit-text-stroke: 2px #171717;
-webkit-text-stroke: 0.24rem rgba(52, 36, 10, 0.95);
paint-order: stroke fill;
}
.cost { display: flex; gap: 2px; flex: 0 0 auto; }
.cost-header { gap: 4px; }
.cost-header .die { width: 64px; height: 64px; }
.cost-header .die b { font-size: 30px; }
/* Body / text zone ----------------------------------------------------------- */
.body {
position: absolute;
inset: auto 0 0 0;
height: var(--body-h);
display: grid;
grid-template-columns: 58px 1fr 58px;
gap: 10px;
padding: 16px 14px;
background: rgba(12, 14, 24, 0.86);
border-top: 2px solid color-mix(in srgb, var(--accent) 60%, transparent);
.skill-text { font-size: 1.78rem; line-height: 1.29; }
.skill-name { font-weight: 800; }
.skill-kind { font-weight: 700; color: var(--ink-soft); }
.skill-sub {
display: block;
margin-top: 0.22rem;
padding-left: 1.1rem;
border-left: 0.2rem solid rgba(52, 36, 10, 0.6);
color: #5c5142;
}
.energy-col, .tags-col {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
padding-top: 4px;
}
.energy { width: 46px; height: 46px; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.8)); }
.tag { display: grid; place-items: center; width: 48px; height: 48px; }
.tag img { width: 100%; height: 100%; }
.tag-faction {
width: 46px;
height: 46px;
border-radius: 50%;
font-size: 11px;
font-weight: 700;
text-align: center;
color: var(--accent);
background: rgba(0, 0, 0, 0.5);
}
/* Skills --------------------------------------------------------------------- */
.skills { display: flex; flex-direction: column; gap: 6px; overflow: hidden; }
.skill {
background: rgba(0, 0, 0, 0.42);
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 11px;
padding: 6px 10px;
}
.skill-head { display: flex; align-items: center; gap: 8px; }
.skill-icon {
width: 34px;
height: 34px;
flex: 0 0 auto;
border-radius: 50%;
background: rgba(0, 0, 0, 0.35);
}
.skill-titles { flex: 1 1 auto; min-width: 0; }
.skill-name { display: block; font-size: 18px; font-weight: 700; line-height: 1.05; }
.skill-kind { display: block; font-size: 12px; font-weight: 600; color: #ffd780; }
.skill-desc { font-size: 14px; line-height: 1.25; margin-top: 4px; }
.skill-sub-name { font-weight: 800; }
.inline-die {
width: 17px;
height: 17px;
vertical-align: -3px;
margin: 0 2px;
width: 1.9rem;
height: 1.9rem;
vertical-align: -0.35rem;
margin: 0 0.2rem;
}
.sub-name { margin-top: 5px; font-size: 13px; font-weight: 700; color: var(--accent); }
.skill-desc.sub { font-size: 13px; margin-top: 1px; }
/* Action body ---------------------------------------------------------------- */
.body-action {
display: flex;
flex-direction: column;
gap: 8px;
padding: 18px 22px;
}
.type-row { display: flex; align-items: center; gap: 10px; }
.type-icon { width: 42px; height: 42px; }
.type-label { font-size: 20px; font-weight: 700; color: var(--accent); }
.desc { font-size: 18px; line-height: 1.34; }
.requirement { margin-top: auto; font-size: 14px; font-style: italic; opacity: 0.75; }
/* Action header -------------------------------------------------------------- */
.header-action {
height: var(--header-action-h);
/* Action card ---------------------------------------------------------------- */
.top-bar {
position: absolute;
top: 3.2rem;
left: 3.4rem;
right: 3.4rem;
display: grid;
grid-template-columns: auto minmax(0, 1fr) auto;
align-items: center;
gap: 14px;
padding: 16px 20px 0;
gap: 1.2rem;
z-index: 3;
}
.header-action .card-name { font-size: 34px; }
.cost-hero {
position: absolute;
left: 2.4rem;
top: 1.4rem;
display: flex;
gap: 0.4rem;
z-index: 3;
}
.cost-hero .die { width: 8.4rem; height: 9.2rem; }
.cost-hero .die b { font-size: 4.5rem; }
.type-badge {
position: absolute;
top: 3.5rem;
right: 2.6rem;
display: flex;
align-items: center;
justify-content: center;
gap: 0.6rem;
min-height: 4rem;
max-width: 17rem;
padding: 0.5rem 1.5rem;
border-radius: 1.3rem;
background: linear-gradient(180deg, #6d5c30 0%, #4f4220 100%);
border: 0.2rem solid var(--gold-line);
box-shadow: inset 0 0 0 0.12rem rgba(255, 255, 255, 0.16),
0 0.2rem 0.5rem rgba(0, 0, 0, 0.35);
color: #fff;
font-size: 1.55rem;
font-weight: 800;
line-height: 1.08;
text-align: center;
z-index: 3;
}
.type-badge img {
flex: 0 0 auto;
width: 2.2rem;
height: 2.2rem;
object-fit: contain;
}
/* Inside the top bar the three pieces flow instead of overlapping. */
.top-bar .plaque,
.top-bar .cost-hero,
.top-bar .type-badge {
position: static;
top: auto;
left: auto;
right: auto;
transform: none;
}
.top-bar .plaque { position: relative; justify-self: center; max-width: 100%; }
.top-bar .cost-hero .die { width: 7.2rem; height: 8rem; }
.top-bar .cost-hero .die b { font-size: 3.9rem; }
.desc { font-size: 1.95rem; line-height: 1.4; }
.requirement {
margin-top: 0.9rem;
font-size: 1.5rem;
font-style: italic;
color: var(--ink-soft);
}
@@ -1,66 +1,37 @@
{% extends "base.html.jinja" %}
{% import "macros.html.jinja" as ui %}
{% block card %}
<img class="art" src="{{ view.illustration }}" alt="">
<div class="shade"></div>
<header class="header">
<div class="hp-bubble">
<div class="hp">
<img src="{{ view.hp_icon }}" alt="PV">
<b>{{ view.hp }}</b>
</div>
<div class="title-block">
<h1 class="card-name">{{ view.name }}</h1>
<ul class="header-tags">
<li>{{ view.element_label }}</li>
<li>{{ view.weapon_label }}</li>
</ul>
</div>
</header>
<section class="body">
<aside class="energy-col" aria-label="Énergie">
{% for _ in range(view.energy) %}
<img class="energy" src="{{ view.energy_icon }}" alt="Énergie">
{% endfor %}
</aside>
{{ ui.plaque(view.name) }}
<div class="skills">
{% for skill in view.skills %}
<article class="skill">
<div class="skill-head">
{% if skill.icon %}<img class="skill-icon" src="{{ skill.icon }}" alt="">{% endif %}
<div class="skill-titles">
<span class="skill-name">{{ skill.name }}</span>
<span class="skill-kind">{{ skill.kind }}</span>
</div>
<div class="cost">
{% for die in skill.cost %}
<span class="die" style="background-image: url('{{ die.icon }}')">
<b>{{ die.count }}</b>
</span>
{% endfor %}
</div>
</div>
<p class="skill-desc">{{ skill.description_html | safe }}</p>
{% if skill.sub_name %}
<p class="sub-name">{{ skill.sub_name }}</p>
<p class="skill-desc sub">{{ skill.sub_description_html | safe }}</p>
{% endif %}
</article>
{% endfor %}
</div>
<aside class="tags-col" aria-label="Attributs">
<aside class="tags" aria-label="Attributs">
{% for tag in view.tags %}
<span class="tag tag-{{ tag.kind }}" title="{{ tag.label }}">
{% if tag.icon %}
<img src="{{ tag.icon }}" alt="{{ tag.label }}">
{% else %}
{{ tag.label }}
{% endif %}
{%- if tag.icon %}
<img class="tag-icon" src="{{ tag.icon }}" alt="">
<span class="tag-label">{{ tag.label }}</span>
{%- else %}
<span class="tag-fallback">{{ tag.label }}</span>
{%- endif %}
</span>
{% endfor %}
</aside>
<section class="panel">
<div class="skills">
{% for skill in view.skills %}
<div class="dice">
{%- for die in skill.cost %}
<span class="die" style="background-image: url('{{ die.icon }}')"><b>{{ die.count }}</b></span>
{%- endfor %}
</div>
<p class="skill-text"><span class="skill-name">【{{ skill.name }}】</span><span class="skill-kind">{{ skill.kind }}</span> {{ skill.description_html | safe }}{% if skill.sub_name %}<span class="skill-sub"><span class="skill-sub-name">{{ skill.sub_name }}</span> {{ skill.sub_description_html | safe }}</span>{% endif %}</p>
{% endfor %}
</div>
</section>
{% endblock %}
@@ -0,0 +1,3 @@
{% macro plaque(name) -%}
<div class="plaque{% if name | length > 24 %} plaque--long{% endif %}{% if name | length > 32 %} plaque--xlong{% endif %}"><span class="plaque-name">{{ name }}</span></div>
{%- endmacro %}
+77 -2
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
from pathlib import Path
from genshin_tcg.data.catalog import Catalog
from genshin_tcg.models import parse_catalog
from genshin_tcg.rendering.context import (
ActionView,
CharacterView,
@@ -62,6 +63,80 @@ def test_character_tags_include_element_and_weapon(sample_catalog: Catalog) -> N
assert view.tags[1].label == "Épée à deux mains"
def test_character_tag_order_matches_the_official_print(
tmp_path: Path, sample_catalog: Catalog
) -> None:
"""The official layout stacks element, nation/faction, then weapon."""
view = build_view(
sample_catalog.get("test_pyro"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
factions={"test_pyro": "liyue"},
width_px=744,
height_px=1039,
)
assert isinstance(view, CharacterView)
assert [tag.kind for tag in view.tags] == ["element", "faction", "weapon"]
def test_none_token_labels_do_not_collide(tmp_path: Path) -> None:
"""``WeaponType.NONE`` ("other weapon") and ``CardType.NONE`` ("event card")
share the ``"none"`` token but must not share a label."""
catalog = Catalog(
cards=tuple(
parse_catalog(
[
{
"_id": 1,
"id": "monster",
"type": "character",
"name": "Monstre",
"hp": 8,
"energy": 0,
"element": "hydro",
"weapon": "none",
"skills": [
{
"_id": 11,
"type": "Attaque normale",
"name": "Charge",
"desc": "Inflige 2 pts de DGT physiques.",
"cost": [{"type": "hydro", "count": 1}],
}
],
},
{
"_id": 2,
"id": "event",
"type": "none",
"name": "Événement",
"skills": {"cost": [], "desc": "Rien."},
},
]
)
)
)
monster = build_view(
catalog.get("monster"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
width_px=744,
height_px=1039,
)
event = build_view(
catalog.get("event"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
width_px=744,
height_px=1039,
)
assert isinstance(monster, CharacterView)
assert isinstance(event, ActionView)
assert monster.weapon_label == "Autre arme"
assert [tag.label for tag in monster.tags if tag.kind == "weapon"] == ["Autre arme"]
assert event.type_label == "Événement"
def test_character_skills_and_costs(sample_catalog: Catalog) -> None:
view = _character(sample_catalog)
assert len(view.skills) == 3
@@ -76,7 +151,7 @@ def test_character_skills_and_costs(sample_catalog: Catalog) -> None:
def test_character_description_is_cleaned_and_highlighted(sample_catalog: Catalog) -> None:
view = _character(sample_catalog)
assert "<br/>" in view.skills[0].description_html
assert '<span style="color: #FFFFFFFF">DGT ' in view.skills[1].description_html
assert '<span style="color: #3a332a">DGT ' in view.skills[1].description_html
assert "inline-die" in view.skills[1].description_html
assert view.skills[1].sub_name == "Flamme éternelle"
assert view.skills[1].sub_description_html is not None
@@ -107,7 +182,7 @@ def test_action_view_fields(sample_catalog: Catalog) -> None:
assert view.requirement == "test_pyro"
assert [die.count for die in view.cost] == [4]
assert "<br/>" in view.description_html
assert '<span style="color: #FFD700">Action de combat</span>' in view.description_html
assert '<span style="color: #988316">Action de combat</span>' in view.description_html
def test_action_without_cost_gets_placeholder_die(sample_catalog: Catalog) -> None:
+18 -1
View File
@@ -2,7 +2,7 @@
from __future__ import annotations
from genshin_tcg.markup import clean_markup, inline_symbols, sanitize, strip_markup
from genshin_tcg.markup import clean_markup, inline_symbols, panel_ink, sanitize, strip_markup
def test_clean_markup_converts_color_tags() -> None:
@@ -19,6 +19,23 @@ def test_clean_markup_keeps_colour_and_underline_spans() -> None:
assert clean_markup(raw) == expected
def test_clean_markup_applies_colour_map() -> None:
raw = '<span style="color: #FFFFFFFF">DGT</span>'
assert clean_markup(raw, color_map=panel_ink) == '<span style="color: #3a332a">DGT</span>'
def test_panel_ink_neutralises_achromatic_and_darkens_hues() -> None:
# White would be invisible on the frosted panel, so it collapses to ink.
# The dataset writes colours as #RRGGBBAA, so the trailing byte is alpha.
assert panel_ink("#FFFFFFFF") == "#3a332a"
assert panel_ink("#FFFFFF") == "#3a332a"
# Chromatic keyword colours keep their hue but gain contrast.
assert panel_ink("#FFD780FF") == "#986F16" # gold
assert panel_ink("#7EC236FF") == "#588826" # Dendro green
# Anything that is not a hex colour is passed through untouched.
assert panel_ink("red") == "red"
def test_sanitize_removes_script_and_event_handlers() -> None:
dirty = '<script>alert(1)</script><span onclick="x()" style="color: red;">ok</span>'
cleaned = sanitize(dirty)