From 88750ed7132c08bcfb24e878c893ef7185c3c0b0 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 10 Oct 2026 15:57:52 +0200 Subject: chore: rewrite tools in revo --- tools/genfonts.py | 41 ---------------------- tools/genfonts.rv | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/subset-ttf.rv | 21 ++++++++++++ tools/subset-ttf.sh | 13 ------- 4 files changed, 118 insertions(+), 54 deletions(-) delete mode 100644 tools/genfonts.py create mode 100644 tools/genfonts.rv create mode 100644 tools/subset-ttf.rv delete mode 100755 tools/subset-ttf.sh (limited to 'tools') diff --git a/tools/genfonts.py b/tools/genfonts.py deleted file mode 100644 index e5afb7e..0000000 --- a/tools/genfonts.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python3 -# this just turns the linxu console fonts which are C arrays into psf1 fonts -# that i can load on disk since half of my psf1 fonts are stolen from there. -# -# usage: genfonts.py [height] - -import re -import sys - - -def parse_c(path): - text = open(path).read() - cut = text.index("}, {") - body = text[cut + 4 :] - body = re.sub(r"/\*.*?\*/", "", body, flags=re.S) - bytes_ = [int(m, 16) for m in re.findall(r"0x([0-9a-fA-F]{2})", body)] - return bytes_ - -def write_psf1(rows, height, out): - count = len(rows) // height - if count < 256: - rows = rows + [0] * (height * (256 - count)) - count = 256 - if count not in (256, 512): - rows = rows[: height * 256] - count = 256 - mode = 0x00 if count == 256 else 0x01 # bit0 set => 512 glyphs - header = bytes([0x36, 0x04, mode, height]) - with open(out, "wb") as f: - f.write(header) - f.write(bytes(rows[: height * count])) - -def main(): - src, out = sys.argv[1], sys.argv[2] - height = int(sys.argv[3]) if len(sys.argv) > 3 else int(re.search(r"8x(\d+)", src).group(1)) - rows = parse_c(src) - write_psf1(rows, height, out) - print(f"{out}: psf1 8x{height}, {len(rows) // height} glyphs in") - -if __name__ == "__main__": - main() diff --git a/tools/genfonts.rv b/tools/genfonts.rv new file mode 100644 index 0000000..6ee6eda --- /dev/null +++ b/tools/genfonts.rv @@ -0,0 +1,97 @@ +# I yoinked a lot of fonts from the linux source code but they are written as C arrays +# which is cool dont get me wrong, C supremacy and all that bullshit, but I need them as +# psf1 fonts, so this converts them and shit +# +# usage: revo genfonts.rv [height] + +fn hexval(character) -> num do + const code = character:ascii() + if (code >= 48 and code <= 57) + return code - 48 + if (code >= 97 and code <= 102) + return code - 87 + if (code >= 65 and code <= 70) + return code - 55 + return 0 +end + +fn read_int(text, start) -> num do + let position = start + let value = 0 + let seen = :false + while (position < text:len() and text[position]:ascii() >= 48 and text[position]:ascii() <= 57) do + value = value * 10 + (text[position]:ascii() - 48) + seen = :true + position += 1 + end + return if (seen) value else 0 +end + +fn parse_c(text) -> table do + const marker = text:index_of("}, {") + let position = if (marker == :nil) 0 else marker + 4 + const length = text:len() + let bytes = {} + + while (position < length) do + if (position + 1 < length and text[position] == "/" and text[position + 1] == "*") do + position += 2 + while (position + 1 < length and (text[position] != "*" or text[position + 1] != "/")) do position += 1 end + position += 2 + end else if (position + 3 < length and text[position] == "0" and text[position + 1] == "x") do + bytes:push(hexval(text[position + 2]) * 16 + hexval(text[position + 3])) + position += 4 + end else do + position += 1 + end + end + + return bytes +end + +fn main() do + if argv:len() < 3 do + panic("usage: genfonts.rv [height]") + end + + const source = argv[1] + const output = argv[2] + + let height = 0 + if (argv:len() > 3) + height = read_int(argv[3], 0) + else do + const marker = source:index_of("8x") + height = if (marker == :nil) 16 else read_int(source, marker + 2) + end + + const handle = fs.open(source, "r"):unwrap() + const text = file.read(handle):unwrap() + file.close(handle) + + let rows = parse_c(text) + let count = math.floor(rows:len() / height) + + if count < 256 do + const padding = height * (256 - count) + for index in 0..padding do rows:push(0) end + count = 256 + end + + if (count != 256 and count != 512) + count = 256 + + const mode = if (count == 256) 0 else 1 + let bytes = { 0x36, 0x04, mode, height } + const total = height * count + for i in 0..total do bytes:push(rows[i]) end + + const data = iter.collect_string(bytes) + const sink = fs.open(output, "w"):unwrap() + file.write(sink, data):unwrap() + file.close(sink) + + print("#{output}: psf1 8x#{height}, #{count} glyphs in") +end + +main() diff --git a/tools/subset-ttf.rv b/tools/subset-ttf.rv new file mode 100644 index 0000000..e795149 --- /dev/null +++ b/tools/subset-ttf.rv @@ -0,0 +1,21 @@ +# trim a ttf down to the printable ascii subset (U+0020..007E) so we only embed +# since revel doesnt actually render all glyphs, some fonts that lung wanted are +# nerd fonts because hes a NERD and are HUGE like my PENIS. +# +# usage: revo subset-ttf.rv + +if argv:len() < 3 do + panic("usage: subset-ttf.rv ") +end + +const in_path = argv[1] +const out_path = argv[2] + +match system({ + "python3", "-m", "fontTools.subset", in_path, + "--unicodes=U+0020-007E", + "--no-hinting", "--glyph-names", + "--output-file=" ~ out_path, +}) + | {:ok, _} => print("wrote " ~ out_path) + | {:err, error} => panic("subset failed (#{error}), need fonttools: python3 -m pip install fonttools") diff --git a/tools/subset-ttf.sh b/tools/subset-ttf.sh deleted file mode 100755 index 70cffa2..0000000 --- a/tools/subset-ttf.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -# trim the ttf into a subset (U+0020..007E), its just to embed fonts that are actually -# rendered, since we have NERD fonts and shit, they can be quite large - -set -e -IN="$1" -OUT="$2" -python3 -m fontTools.subset "$IN" \ - --unicodes=U+0020-007E \ - --no-hinting --glyph-names \ - --output-file="$OUT" -echo "wrote $OUT" -- cgit v1.3