diff --git a/src/Gjallar/Gjallar.csproj b/src/Gjallar/Gjallar.csproj index 830da2d..eb219de 100644 --- a/src/Gjallar/Gjallar.csproj +++ b/src/Gjallar/Gjallar.csproj @@ -11,4 +11,10 @@ + + + PreserveNewest + PreserveNewest + + diff --git a/src/Gjallar/Program.cs b/src/Gjallar/Program.cs index a33e845..aa8d252 100644 --- a/src/Gjallar/Program.cs +++ b/src/Gjallar/Program.cs @@ -655,6 +655,14 @@ internal sealed class GjallarRenderer : IDisposable framebuffer = new { framebuffer.Width, framebuffer.Height, bytes = framebuffer.BufferBytes }, refreshHz = config.RefreshHz, measuredFps = Math.Round(measuredFps, 2), + fonts = new + { + requestedPath = config.FontPath, + packagedKanaPath = Path.Combine(AppContext.BaseDirectory, "assets", "fonts", "GjallarKana8x16.psf"), + defaultFont = new { width = fonts.Default.Width, height = fonts.Default.Height, kana = fonts.Default.SupportsKana }, + edgeFont = new { width = fonts.Edge.Width, height = fonts.Edge.Height, kana = fonts.Edge.SupportsKana }, + loaded = fonts.Describe(), + }, receive = new { status = lastReceiveStatus, @@ -2327,6 +2335,9 @@ internal sealed class FontAtlas public PsfFont Edge => fonts.FirstOrDefault(font => font.Height >= 14 && font.Width >= 8) ?? fonts.FirstOrDefault(font => font.Height >= 10 && font.Width >= 6) ?? fonts.FirstOrDefault(font => font.Height >= 5 && font.Width >= 3) ?? Default; public PsfFont this[int index] => fonts[Math.Clamp(index, 0, fonts.Length - 1)]; public int IndexOf(PsfFont font) => Math.Max(0, Array.IndexOf(fonts, font)); + public object[] Describe() => fonts + .Select(font => (object)new { width = font.Width, height = font.Height, kana = font.SupportsKana }) + .ToArray(); private FontAtlas(PsfFont[] fonts) => this.fonts = fonts .OrderBy(font => font.Width * font.Height) @@ -2339,6 +2350,8 @@ internal sealed class FontAtlas var candidates = new[] { primary, + Path.Combine(AppContext.BaseDirectory, "assets", "fonts", "GjallarKana8x16.psf"), + Path.Combine(AppContext.BaseDirectory, "assets", "fonts", "Unifont-APL8x16.psf.gz"), "/usr/share/consolefonts/Unifont-APL8x16.psf.gz", "/usr/share/consolefonts/Lat2-Terminus12x6.psf.gz", "/usr/share/consolefonts/Lat7-Terminus12x6.psf.gz", @@ -2359,6 +2372,10 @@ internal sealed class FontAtlas .GroupBy(font => (font.Width, font.Height)) .Select(group => group.OrderByDescending(font => font.SupportsKana).First()) .ToArray(); + if (loaded.Any(font => font.SupportsKana)) + { + loaded = loaded.Where(font => font.SupportsKana).ToArray(); + } if (loaded.Length == 0) { throw new InvalidOperationException("No PSF console fonts found; pass --font."); @@ -2473,7 +2490,11 @@ internal sealed class PsfFont } public bool Supports(char ch) => unicodeMap.ContainsKey(ch); - public bool SupportsKana => Supports('あ') && Supports('ん') && Supports('ア') && Supports('ン'); + public bool SupportsKana => + Supports('\u3042') && + Supports('\u3093') && + Supports('\u30A2') && + Supports('\u30F3'); public PsfFont ShrinkTo(int width, int height) { diff --git a/src/Gjallar/assets/fonts/GjallarKana8x16.psf b/src/Gjallar/assets/fonts/GjallarKana8x16.psf new file mode 100644 index 0000000..45840f7 Binary files /dev/null and b/src/Gjallar/assets/fonts/GjallarKana8x16.psf differ diff --git a/src/Gjallar/assets/fonts/Unifont-APL8x16.psf.gz b/src/Gjallar/assets/fonts/Unifont-APL8x16.psf.gz new file mode 100644 index 0000000..686a7c4 Binary files /dev/null and b/src/Gjallar/assets/fonts/Unifont-APL8x16.psf.gz differ diff --git a/tools/generate_psf2_from_ttf.py b/tools/generate_psf2_from_ttf.py new file mode 100644 index 0000000..02d8759 --- /dev/null +++ b/tools/generate_psf2_from_ttf.py @@ -0,0 +1,116 @@ +from __future__ import annotations + +import argparse +import struct +from pathlib import Path + +from PIL import Image, ImageDraw, ImageFont + + +PSF2_MAGIC = 0x864AB572 +UNICODE_TABLE_FLAG = 0x01 + + +def main() -> None: + parser = argparse.ArgumentParser(description="Generate a fixed-cell PSF2 font from a TTF/OTF font.") + parser.add_argument("--font", required=True, help="Source TTF/OTF path.") + 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.") + args = parser.parse_args() + + font_path = Path(args.font) + out_path = Path(args.out) + width = args.width + height = args.height + + codepoints = [0x0000] + codepoints.extend(range(0x0020, 0x007F)) + codepoints.extend(range(0x00A0, 0x0100)) + codepoints.extend(range(0x2000, 0x2070)) + codepoints.extend(range(0x3000, 0x3040)) + codepoints.extend(range(0x3040, 0x30A0)) + 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] + + bytes_per_row = (width + 7) // 8 + char_size = bytes_per_row * height + header = struct.pack( + " 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: + ch = chr(codepoint) + image = Image.new("L", (width, height), 0) + draw = ImageDraw.Draw(image) + 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) + draw.text((x, y), ch, fill=255, font=font) + + bytes_per_row = (width + 7) // 8 + packed = bytearray(bytes_per_row * height) + pixels = image.load() + for y in range(height): + for x in range(width): + if pixels[x, y] < 96: + continue + offset = y * bytes_per_row + x // 8 + packed[offset] |= 0x80 >> (x % 8) + return bytes(packed) + + +if __name__ == "__main__": + main()