Files
2026-08-09 21:54:29 +03:00

156 lines
6.7 KiB
Python

#!/usr/bin/env python3
"""Derive the icon masters and the in-page mark geometry from the hat trace.
`assets/hat-trace.svg` is potrace output: a 722x436 viewBox offset to "160 280", with
the path in 10x units and a flipped Y (`translate(0,1280) scale(0.1,-0.1)`).
Everything downstream speaks the 24x24 box — src/lib/mark.ts and the crowned
marks in src/apps/*/icon.ts — so this collapses the trace's coordinate system
and the fit into a single transform per output.
Run after editing the trace: python3 scripts/refit-hat.py
Then rasterise the PNGs: npm run icons
Outputs (all generated, do not hand-edit):
assets/icon.svg 48/128 master — full artwork, bells drawn as rings
assets/icon-small.svg 16/32 master — see SMALL below
src/lib/hat-path.ts the same geometry for marks drawn inside the pages
"""
import pathlib
import re
ROOT = pathlib.Path(__file__).resolve().parent.parent
TRACE = ROOT / "assets/hat-trace.svg"
# The trace's own frame, i.e. the viewBox as authored.
VB_X, VB_Y, VB_W, VB_H = 160.0, 280.0, 722.0, 436.0
# LARGE: the hat sits 21 units wide, centred, which keeps its optical width
# close to the line-art mark this replaced (20.2) without crowding the edges.
LARGE_INK = 21.0
# SMALL: the hat is wide and squat — aspect 1.76 against the old mark's 1.25 —
# so fitted whole it fills only 45% of a square box and turns to mush at 16px.
# The small master therefore overflows the box to 30 units and lets the outer
# bells clip, which buys back the height (75% of the box), and adds a stroke to
# thicken hairlines that would otherwise fall below a pixel. It also drops the
# bells' counters so they render solid: a ring's hole is sub-pixel at this size
# and only muddies the shape.
SMALL_INK = 30.0
SMALL_STROKE = 120 # in trace units, applied before the scale down
SMALL_SUBPATHS = 4 # the first four; 5 and 6 are the outer bells' counters
PINK = "#c62f66"
def fit(ink_w: float) -> tuple[str, float, float]:
"""One transform mapping raw trace units into the 24x24 box, plus the
resulting ink height and bottom edge (which the crowned marks anchor on)."""
s = ink_w / VB_W
ink_h = VB_H * s
ox = (24.0 - ink_w) / 2.0
oy = (24.0 - ink_h) / 2.0
# raw -> potrace -> box, collapsed:
# x = 0.1*s*x_raw - VB_X*s + ox
# y = -0.1*s*y_raw + (1280 - VB_Y)*s + oy
tx = -VB_X * s + ox
ty = (1280.0 - VB_Y) * s + oy
k = 0.1 * s
return f"translate({tx:.5f} {ty:.5f}) scale({k:.8f} -{k:.8f})", ink_h, oy + ink_h
def main() -> None:
raw = TRACE.read_text()
d = " ".join(re.search(r'<path d="(.*?)"', raw, re.S).group(1).split())
subpaths = [p for p in d.split("z") if p.strip()]
solid = "z".join(subpaths[:SMALL_SUBPATHS]) + "z"
large_xform, large_h, large_bottom = fit(LARGE_INK)
small_xform, small_h, _ = fit(SMALL_INK)
# Half the stroke sits outside the outline, in box units, so the mark's
# viewBox has to give it room or the bells clip against their own slot.
stroke_pad = (SMALL_STROKE / 2) * (0.1 * LARGE_INK / VB_W)
(ROOT / "assets/icon.svg").write_text(f"""<!--
Jestr Apps extension icon — 48px / 128px master. GENERATED, see below.
A silhouette traced from the jestr hat, normalised into the same 24x24 box the
in-page marks use (src/lib/mark.ts), so the hat in a page and the hat in the
toolbar are one drawing. The inner group is the trace's coordinate system —
10x units, flipped Y — mapped into that box.
Regenerate with `python3 scripts/refit-hat.py`; do not hand-tune the numbers.
Rasterise the PNGs in public/ with `npm run icons`.
-->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">
<g fill="{PINK}">
<g transform="{large_xform}">
<path d="{d}"/>
</g>
</g>
</svg>
""")
(ROOT / "assets/icon-small.svg").write_text(f"""<!--
Jestr Apps extension icon — 16px / 32px master. GENERATED, see below.
Same drawing as icon.svg, refit for sizes where the master washes out. The hat
is wide and squat (aspect 1.76), so fitted whole it uses under half a square
box's height and mushes at 16px. Here it overflows to {SMALL_INK:.0f} units and lets the
outer bells clip, which buys the height back, carries a {SMALL_STROKE}-unit stroke to hold
the hairlines above a pixel, and drops the bells' counters so they read solid
rather than as rings whose holes fall below the grid.
Regenerate with `python3 scripts/refit-hat.py`; do not hand-tune the numbers.
-->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">
<g fill="{PINK}" stroke="{PINK}" stroke-width="{SMALL_STROKE}" stroke-linejoin="round">
<g transform="{small_xform}">
<path d="{solid}"/>
</g>
</g>
</svg>
""")
(ROOT / "src/lib/hat-path.ts").write_text(f'''// GENERATED by scripts/refit-hat.py from assets/hat-trace.svg — do not edit by hand.
//
// The hat as geometry the in-page marks can wear, in the same 24x24 box as the
// toolbar icon so the two are one drawing. `HAT_FIT` carries the trace's own
// coordinate system (10x units, flipped Y) into that box.
/** Maps the traced path's raw units into the 24x24 box. */
export const HAT_FIT = '{large_xform}'
/** Where the fitted hat's ink actually lands in the box. The crowned marks in
* apps/*\\/icon.ts anchor on `BOTTOM`, so they follow if the fit ever changes. */
export const HAT_INK = {{ top: {24 - large_bottom:.4f}, bottom: {large_bottom:.4f}, height: {large_h:.4f} }}
/** The ink's own bounds as a viewBox, with room for HAT_STROKE. A mark standing
* alone wants this rather than the full 24x24: the hat is wide and squat, so in
* the square box it fills barely half the height and would sit visibly small
* inside a square slot. */
export const HAT_VIEWBOX = '{(24 - LARGE_INK) / 2 - stroke_pad:.4f} {24 - large_bottom - stroke_pad:.4f} {LARGE_INK + 2 * stroke_pad:.4f} {large_h + 2 * stroke_pad:.4f}'
/** The traced outline, in its own units. Wrap it in HAT_FIT to place it. */
export const HAT_PATH = '<path d="{d}"/>'
/** The same outline without the outer bells' counters, so those read as solid
* discs. Every mark drawn inside a page lands in an 18-30px slot, where a
* ring's hole is under a pixel and only muddies the shape. */
export const HAT_PATH_SOLID = '<path d="{solid}"/>'
/** Stroke that thickens HAT_PATH_SOLID's hairlines, in the path's own units, so
* it scales with the shape rather than with the slot. Same weight the 16/32
* icon master carries, for the same reason. */
export const HAT_STROKE = {SMALL_STROKE}
''')
print(f"large: ink {LARGE_INK} wide, {large_h:.2f} tall, bottom at {large_bottom:.4f}")
print(f"small: ink {SMALL_INK} wide, {small_h:.2f} tall (clipped), stroke {SMALL_STROKE}")
print("wrote assets/icon.svg, assets/icon-small.svg, src/lib/hat-path.ts")
if __name__ == "__main__":
main()