Files
DoubleTroubleRecomp/tools/gen_font.py
T
linkatplugandClaude Opus 5.5 90c398d531 Son de l'intro (voix 16 bits) et menu de réglages F12
- audio.c : flux 16 bits non signés (voix de l'intro, qui saturaient),
  pause du mixeur pendant le menu sauf les sons en boucle.
- ail.c : retrait de la trace de débogage.
- menu.c/menu.h : menu en surimpression indépendant de SDL (volumes,
  plein écran, taille de fenêtre, lissage, format 4:3) et fichier de réglages.
- menu_font.h : police Roboto (Apache 2.0) générée par tools/gen_font.py.
- host_sdl.c : F12 ouvre le menu ; le temps du jeu est figé pendant le menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 02:55:52 +01:00

122 lines
5.8 KiB
Python

"""Génère runtime/menu_font.h : glyphes anticrénelés (niveaux de gris) pour le menu de réglages.
Rastérise une police installée sous Windows avec GDI (ctypes, sans dépendance) et écrit
un en-tête C portable : le runtime n'a plus besoin de la police ni de GDI.
Police par défaut : Roboto (licence Apache 2.0).
python tools/gen_font.py [sortie] [famille]
"""
import ctypes
import ctypes.wintypes as wt
import sys
OUT = sys.argv[1] if len(sys.argv) > 1 else "runtime/menu_font.h"
FAMILY = sys.argv[2] if len(sys.argv) > 2 else "Roboto"
CHARS = "".join(chr(c) for c in range(32, 127)) + "éèêëàâäçôöîïùûüœÉÈÊÀÂÇÔÎ×←→↑↓·…–°"
SIZES = [("small", 22, 400), ("normal", 30, 400), ("title", 46, 700)] # hauteur de cellule en pixels
gdi = ctypes.WinDLL("gdi32")
user = ctypes.WinDLL("user32")
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [("biSize", wt.DWORD), ("biWidth", wt.LONG), ("biHeight", wt.LONG), ("biPlanes", wt.WORD),
("biBitCount", wt.WORD), ("biCompression", wt.DWORD), ("biSizeImage", wt.DWORD),
("biXPelsPerMeter", wt.LONG), ("biYPelsPerMeter", wt.LONG), ("biClrUsed", wt.DWORD),
("biClrImportant", wt.DWORD)]
class TEXTMETRICW(ctypes.Structure):
_fields_ = [("tmHeight", wt.LONG), ("tmAscent", wt.LONG), ("tmDescent", wt.LONG),
("tmInternalLeading", wt.LONG), ("tmExternalLeading", wt.LONG), ("tmAveCharWidth", wt.LONG),
("tmMaxCharWidth", wt.LONG), ("tmWeight", wt.LONG), ("tmOverhang", wt.LONG),
("tmDigitizedAspectX", wt.LONG), ("tmDigitizedAspectY", wt.LONG), ("tmFirstChar", wt.WCHAR),
("tmLastChar", wt.WCHAR), ("tmDefaultChar", wt.WCHAR), ("tmBreakChar", wt.WCHAR),
("tmItalic", ctypes.c_byte), ("tmUnderlined", ctypes.c_byte), ("tmStruckOut", ctypes.c_byte),
("tmPitchAndFamily", ctypes.c_byte), ("tmCharSet", ctypes.c_byte)]
gdi.CreateFontW.restype = wt.HANDLE
gdi.CreateFontW.argtypes = [ctypes.c_int] * 5 + [wt.DWORD] * 8 + [wt.LPCWSTR]
gdi.CreateCompatibleDC.restype = wt.HANDLE
gdi.CreateCompatibleDC.argtypes = [wt.HANDLE]
gdi.CreateDIBSection.restype = wt.HANDLE
gdi.CreateDIBSection.argtypes = [wt.HANDLE, ctypes.c_void_p, wt.UINT, ctypes.POINTER(ctypes.c_void_p), wt.HANDLE, wt.DWORD]
gdi.SelectObject.restype = wt.HANDLE
gdi.SelectObject.argtypes = [wt.HANDLE, wt.HANDLE]
gdi.DeleteObject.argtypes = [wt.HANDLE]
gdi.DeleteDC.argtypes = [wt.HANDLE]
gdi.SetTextColor.argtypes = [wt.HANDLE, wt.DWORD]
gdi.SetBkColor.argtypes = [wt.HANDLE, wt.DWORD]
gdi.TextOutW.argtypes = [wt.HANDLE, ctypes.c_int, ctypes.c_int, wt.LPCWSTR, ctypes.c_int]
gdi.GetTextExtentPoint32W.argtypes = [wt.HANDLE, wt.LPCWSTR, ctypes.c_int, ctypes.POINTER(wt.SIZE)]
gdi.GetTextMetricsW.argtypes = [wt.HANDLE, ctypes.POINTER(TEXTMETRICW)]
gdi.GetTextFaceW.argtypes = [wt.HANDLE, ctypes.c_int, wt.LPWSTR]
gdi.GdiFlush.argtypes = []
def render(cell, weight):
dc = gdi.CreateCompatibleDC(None)
font = gdi.CreateFontW(-cell * 3 // 4, 0, 0, 0, weight, 0, 0, 0, 1, 4, 0, 4, 0, FAMILY)
gdi.SelectObject(dc, font)
face = ctypes.create_unicode_buffer(64)
gdi.GetTextFaceW(dc, 64, face)
if face.value.lower() != FAMILY.lower():
sys.exit("police %r introuvable (GDI propose %r)" % (FAMILY, face.value))
tm = TEXTMETRICW()
gdi.GetTextMetricsW(dc, ctypes.byref(tm))
h = tm.tmHeight
pad = h // 2
W = 4 * h
bmi = BITMAPINFOHEADER(ctypes.sizeof(BITMAPINFOHEADER), W, -h, 1, 32, 0, 0, 0, 0, 0, 0)
bits = ctypes.c_void_p()
dib = gdi.CreateDIBSection(dc, ctypes.byref(bmi), 0, ctypes.byref(bits), None, 0)
gdi.SelectObject(dc, dib)
gdi.SetTextColor(dc, 0xffffff)
gdi.SetBkColor(dc, 0)
buf = (ctypes.c_uint32 * (W * h)).from_address(bits.value)
glyphs = []
for ch in CHARS:
sz = wt.SIZE()
gdi.GetTextExtentPoint32W(dc, ch, 1, ctypes.byref(sz))
ctypes.memset(bits, 0, W * h * 4)
gdi.TextOutW(dc, pad, 0, ch, 1)
gdi.GdiFlush()
cov = [[(buf[y * W + x] >> 8) & 255 for x in range(W)] for y in range(h)]
cols = [x for x in range(W) if any(cov[y][x] for y in range(h))]
if cols:
x0, x1 = cols[0], cols[-1] + 1
else:
x0 = x1 = pad
glyphs.append((ord(ch), sz.cx, x0 - pad, x1 - x0, [row[x0:x1] for row in cov]))
gdi.DeleteObject(dib)
gdi.DeleteObject(font)
gdi.DeleteDC(dc)
return h, tm.tmAscent, glyphs
out = ["/* Généré par tools/gen_font.py : ne pas modifier à la main.",
" * Glyphes de la police %s (licence Apache 2.0), couverture 0..255. */" % FAMILY,
"#pragma once", "#include <stdint.h>", "",
"typedef struct { uint16_t code; int8_t xoff; uint8_t adv, w; uint32_t off; } MenuGlyph;",
"typedef struct { int height, ascent, count; const MenuGlyph *glyphs; const uint8_t *data; } MenuFont;", ""]
for name, cell, weight in SIZES:
h, asc, glyphs = render(cell, weight)
data, table = [], []
for code, adv, xoff, w, rows in glyphs:
table.append("{%d,%d,%d,%d,%d}" % (code, xoff, adv, w, len(data)))
for r in rows:
data.extend(r)
out.append("static const uint8_t font_%s_data[%d] = {" % (name, max(1, len(data))))
for i in range(0, len(data), 40):
out.append(" " + ",".join(str(v) for v in data[i:i + 40]) + ",")
out.append("};")
out.append("static const MenuGlyph font_%s_glyphs[%d] = {" % (name, len(table)))
for i in range(0, len(table), 8):
out.append(" " + ",".join(table[i:i + 8]) + ",")
out.append("};")
out.append("static const MenuFont font_%s = {%d, %d, %d, font_%s_glyphs, font_%s_data};" % (name, h, asc, len(table), name, name))
out.append("")
print("%s : hauteur %d, %d glyphes, %d octets" % (name, h, len(table), len(data)))
open(OUT, "w", encoding="utf-8", newline="\n").write("\n".join(out))