Tighten Gjallar fixed-cell font usage

This commit is contained in:
Meta Vens 2026-06-11 21:13:11 +01:00
commit 29dc7794fc
3 changed files with 43 additions and 38 deletions

View file

@ -2399,8 +2399,7 @@ internal sealed class FontAtlas
var source = loaded.OrderByDescending(font => font.SupportsKana).ThenBy(font => font.Width * font.Height).First();
var generated = new[]
{
source.ShrinkTo(6, 10),
source.ShrinkTo(7, 11),
source.ShrinkTo(8, 12),
source,
};
return new FontAtlas(generated
@ -2416,7 +2415,7 @@ internal sealed class FontAtlas
var candidates = needsKana && fonts.Any(font => font.SupportsKana)
? fonts.Where(font => font.SupportsKana)
: fonts;
var weightedPressure = Math.Clamp(weight / 4.0f, 0.0f, 5.0f);
var weightedPressure = Math.Clamp(weight / 6.0f, 0.0f, 2.5f);
foreach (var font in candidates.OrderByDescending(font => font.Width * font.Height))
{
var columns = Math.Max(1, width / Math.Max(1, font.Width));
@ -2435,7 +2434,7 @@ internal sealed class FontAtlas
public PsfFont HeaderFor(int height, string text = "")
{
var target = height < 40 ? Math.Max(3, height / 3) : Math.Max(10, Math.Min(24, height / 7));
var target = height < 40 ? Math.Max(4, height / 2) : Math.Max(10, Math.Min(24, height / 5));
var candidates = ContainsKana(text) && fonts.Any(font => font.SupportsKana)
? fonts.Where(font => font.SupportsKana)
: fonts;
@ -2475,8 +2474,8 @@ internal sealed class PsfFont
public int Width { get; }
public int Height { get; }
public int BytesPerRow { get; }
public int LineHeight => Height + (Height <= 5 ? 1 : 2);
public int BlockSpacing => Height <= 5 ? 0 : Math.Max(1, Height / 3);
public int LineHeight => Height + 1;
public int BlockSpacing => Height <= 5 ? 0 : 1;
private PsfFont(int width, int height, byte[][] glyphs, IReadOnlyDictionary<int, int>? unicodeMap = null)
{

View file

@ -17,6 +17,9 @@ def main() -> None:
parser.add_argument("--out", required=True, help="Output PSF2 path.")
parser.add_argument("--width", type=int, default=8, help="Cell width in pixels.")
parser.add_argument("--height", type=int, default=16, help="Cell height in pixels.")
parser.add_argument("--padding-x", type=int, default=1, help="Horizontal cell padding in pixels.")
parser.add_argument("--padding-y", type=int, default=1, help="Vertical cell padding in pixels.")
parser.add_argument("--source-size", type=int, default=96, help="Large source font size used before downscaling into the target cell.")
args = parser.parse_args()
font_path = Path(args.font)
@ -33,9 +36,11 @@ def main() -> None:
codepoints.extend(range(0x30A0, 0x3100))
codepoints.extend(range(0xFF00, 0xFFEF + 1))
font_size = choose_font_size(font_path, width, height)
font = ImageFont.truetype(str(font_path), font_size)
glyphs = [render_glyph(font, cp, width, height) for cp in codepoints]
font = ImageFont.truetype(str(font_path), args.source_size)
glyphs = [
render_glyph(font, cp, width, height, args.padding_x, args.padding_y)
for cp in codepoints
]
bytes_per_row = (width + 7) // 8
char_size = bytes_per_row * height
@ -63,46 +68,47 @@ def main() -> None:
handle.write(unicode_table)
print(
f"wrote {out_path} from {font_path.name} at {font_size}px "
f"wrote {out_path} from {font_path.name} at source {args.source_size}px "
f"with {len(glyphs)} glyphs ({width}x{height})"
)
def choose_font_size(font_path: Path, width: int, height: int) -> int:
samples = [
"A", "M", "0", "?", "あ", "ん", "ア", "ン", "メ", "タ", "め",
]
for size in range(height * 2, 1, -1):
font = ImageFont.truetype(str(font_path), size)
if all(fits(font, sample, width, height) for sample in samples):
return size
raise RuntimeError(f"Could not fit representative glyphs from {font_path} into {width}x{height}.")
def fits(font: ImageFont.FreeTypeFont, text: str, width: int, height: int) -> bool:
bbox = font.getbbox(text)
if bbox is None:
return True
left, top, right, bottom = bbox
return (right - left) <= width and (bottom - top) <= height
def render_glyph(font: ImageFont.FreeTypeFont, codepoint: int, width: int, height: int) -> bytes:
def render_glyph(
font: ImageFont.FreeTypeFont,
codepoint: int,
width: int,
height: int,
padding_x: int,
padding_y: int,
) -> bytes:
ch = chr(codepoint)
image = Image.new("L", (width, height), 0)
draw = ImageDraw.Draw(image)
work = Image.new("L", (font.size * 2, font.size * 2), 0)
draw = ImageDraw.Draw(work)
bbox = font.getbbox(ch)
if bbox is not None:
left, top, right, bottom = bbox
glyph_width = max(0, right - left)
glyph_height = max(0, bottom - top)
x = max(0, (width - glyph_width) // 2 - left)
y = max(0, (height - glyph_height) // 2 - top)
x = max(0, (work.width - (right - left)) // 2 - left)
y = max(0, (work.height - (bottom - top)) // 2 - top)
draw.text((x, y), ch, fill=255, font=font)
glyph_box = work.getbbox()
if glyph_box is None:
fitted = Image.new("L", (width, height), 0)
else:
cropped = work.crop(glyph_box)
inner_width = max(1, width - padding_x * 2)
inner_height = max(1, height - padding_y * 2)
scale = min(inner_width / cropped.width, inner_height / cropped.height)
scaled_width = max(1, min(inner_width, round(cropped.width * scale)))
scaled_height = max(1, min(inner_height, round(cropped.height * scale)))
resampled = cropped.resize((scaled_width, scaled_height), Image.Resampling.LANCZOS)
fitted = Image.new("L", (width, height), 0)
paste_x = max(0, (width - scaled_width) // 2)
paste_y = max(0, (height - scaled_height) // 2)
fitted.paste(resampled, (paste_x, paste_y))
bytes_per_row = (width + 7) // 8
packed = bytearray(bytes_per_row * height)
pixels = image.load()
pixels = fitted.load()
for y in range(height):
for x in range(width):
if pixels[x, y] < 96: