43 lines
2.1 KiB
Python
43 lines
2.1 KiB
Python
"""Génère les favicons « Sh » à partir des glyphes Epilogue variable.
|
|
Usage : python3 favicon-gen.py <dossier de sortie>"""
|
|
import sys, os
|
|
from fontTools.ttLib import TTFont
|
|
from fontTools.varLib.instancer import instantiateVariableFont
|
|
from fontTools.pens.svgPathPen import SVGPathPen
|
|
from fontTools.pens.boundsPen import BoundsPen
|
|
F = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../fonts/epilogue/")
|
|
UP, IT = "Epilogue-VariableFont_wght.ttf", "Epilogue-Italic-VariableFont_wght.ttf"
|
|
|
|
def glyph(font, ch, w):
|
|
f = instantiateVariableFont(TTFont(F + font), {"wght": w})
|
|
gs = f.getGlyphSet(); name = f.getBestCmap()[ord(ch)]
|
|
p = SVGPathPen(gs); gs[name].draw(p)
|
|
b = BoundsPen(gs); gs[name].draw(b)
|
|
return p.getCommands(), b.bounds
|
|
|
|
def make(out, letters, gap=40):
|
|
parts, x = [], None
|
|
for font, ch, w in letters:
|
|
d, b = glyph(font, ch, w)
|
|
dx = -b[0] if x is None else x + gap - b[0]
|
|
parts.append((d, dx, b)); x = dx + b[2]
|
|
x0 = 0; W = x
|
|
y0 = min(b[1] for _, _, b in parts); y1 = max(b[3] for _, _, b in parts); H = y1 - y0
|
|
S, pad = 1000, 140; sc = (S - 2 * pad) / max(W, H)
|
|
tx = pad + ((S - 2 * pad) - W * sc) / 2
|
|
ty = pad + ((S - 2 * pad) - H * sc) / 2 + y1 * sc
|
|
paths = "".join(f'<path transform="translate({dx:.1f} 0)" d="{d}"/>' for d, dx, _ in parts)
|
|
open(out, "w").write(
|
|
f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {S} {S}">'
|
|
f'<rect width="{S}" height="{S}" rx="180" fill="#fff"/>'
|
|
f'<g fill="#000" transform="translate({tx:.1f} {ty:.1f}) scale({sc:.4f} {-sc:.4f})">{paths}</g></svg>')
|
|
|
|
o = sys.argv[1]
|
|
# S italique très gras, h droit fin (contraste du logo « Le Shed »).
|
|
make(o + "/favicon.svg", [(IT, "S", 900), (UP, "h", 100)]) # 180 px et plus
|
|
make(o + "/favicon-tab.svg", [(IT, "S", 900), (UP, "h", 300)]) # onglets
|
|
make(o + "/favicon-small.svg", [(IT, "S", 900), (UP, "h", 400)]) # ico 16/32
|
|
# variante inverse, pour comparaison
|
|
make(o + "/alt.svg", [(IT, "S", 100), (UP, "h", 900)])
|
|
make(o + "/alt-small.svg", [(IT, "S", 400), (UP, "h", 900)])
|