From c9fd9181700493823d6947af9ab7ec56c20b7f36 Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Sun, 4 Jun 2023 23:33:45 +0200
Subject: user/vterm: vendor a font (spleen) instead of downloading one on
 build

---
 src/user/app/init/init.c   |  2 +-
 src/user/app/vterm/font.c  | 34 ++++++++++++++++++++--------------
 src/user/app/vterm/vterm.c |  2 +-
 src/user/app/vterm/vterm.h |  9 +++++++--
 4 files changed, 29 insertions(+), 18 deletions(-)

(limited to 'src/user')

diff --git a/src/user/app/init/init.c b/src/user/app/init/init.c
index 8346a31..b9219ec 100644
--- a/src/user/app/init/init.c
+++ b/src/user/app/init/init.c
@@ -74,7 +74,7 @@ int main(void) {
 		execv(argv[0], (void*)argv);
 	}
 	MOUNT_AT("/vtty") {
-		const char *allow[] = {"/bin/vterm", "/kdev/video/", "/keyboard", "/init/font.psf", NULL};
+		const char *allow[] = {"/bin/vterm", "/kdev/video/", "/keyboard", "/init/usr/share/fonts/", NULL};
 		const char *argv[] = {"/bin/vterm", NULL};
 		MOUNT_AT("/") { fs_whitelist(allow); }
 		execv(argv[0], (void*)argv);
diff --git a/src/user/app/vterm/font.c b/src/user/app/vterm/font.c
index e4f124f..777b094 100644
--- a/src/user/app/vterm/font.c
+++ b/src/user/app/vterm/font.c
@@ -1,16 +1,16 @@
 #include "vterm.h"
+#include <err.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-struct psf font;
+struct psf2 font;
 void *font_data;
 
 void font_load(const char *path) {
 	FILE *f = fopen(path, "r");
 	if (!f) {
-		eprintf("couldn't open font \"%s\"", path);
-		exit(1);
+		err(1, "couldn't open font \"%s\"", path);
 	}
 
 	void *buf;
@@ -19,30 +19,36 @@ void font_load(const char *path) {
 	fseek(f, 0, SEEK_END);
 	buflen = ftell(f);
 	if (buflen < 0) {
-		eprintf("can't get the size of \"%s\"", path);
-		exit(1);
+		errx(1, "can't get the size of \"%s\"", path);
 	}
 
 	buf = malloc(buflen);
 	if (!buf) {
-		eprintf("out of memory");
-		exit(1);
+		err(1, "out of memory");
 	}
 
 	fseek(f, 0, SEEK_SET);
 	fread(buf, 1, buflen, f);
 	if (ferror(f)) {
-		eprintf("error reading file");
-		exit(1);
+		err(1, "error reading font");
 	}
 	fclose(f);
 
-	if (memcmp(buf, "\x72\xb5\x4a\x86", 4)) {
-		eprintf("invalid psf header");
-		exit(1);
+	if (!memcmp(buf, "\x72\xb5\x4a\x86", 4)) {
+		memcpy(&font, buf, sizeof font);
+		font_data = buf + font.glyph_offset;
+	} else if (!memcmp(buf, "\x36\x04", 2)) {
+		struct psf1 *hdr = buf;
+		font = (struct psf2){
+			.glyph_amt = 256,
+			.glyph_size = hdr->h,
+			.h = hdr->h,
+			.w = 8,
+		};
+		font_data = buf + 4;
+	} else {
+		errx(1, "invalid psf header");
 	}
-	memcpy(&font, buf, sizeof font);
-	font_data = buf + font.glyph_offset;
 }
 
 void font_blit(uint32_t glyph, int x, int y) {
diff --git a/src/user/app/vterm/vterm.c b/src/user/app/vterm/vterm.c
index 76f6a17..a7d5bcc 100644
--- a/src/user/app/vterm/vterm.c
+++ b/src/user/app/vterm/vterm.c
@@ -35,7 +35,7 @@ int main(void) {
 		eprintf("fb_setup error");
 		return 1;
 	}
-	font_load("/init/font.psf");
+	font_load("/init/usr/share/fonts/spleen/spleen-8x16.psfu");
 
 	static char buf[512];
 	struct ufs_request res;
diff --git a/src/user/app/vterm/vterm.h b/src/user/app/vterm/vterm.h
index 72c1fd0..026e71a 100644
--- a/src/user/app/vterm/vterm.h
+++ b/src/user/app/vterm/vterm.h
@@ -7,7 +7,12 @@
 #define eprintf(fmt, ...) fprintf(stderr, "vterm: "fmt"\n" __VA_OPT__(,) __VA_ARGS__)
 
 
-struct psf {
+struct psf1 {
+    uint16_t magic;
+	uint8_t mode;
+	uint8_t h;
+} __attribute__((packed));
+struct psf2 {
     uint32_t magic;
     uint32_t version;
     uint32_t glyph_offset;
@@ -17,7 +22,7 @@ struct psf {
     uint32_t h;
     uint32_t w;
 } __attribute__((packed));
-extern struct psf font;
+extern struct psf2 font;
 extern void *font_data;
 void font_load(const char *path);
 void font_blit(uint32_t glyph, int x, int y);
-- 
cgit v1.2.3