summaryrefslogtreecommitdiff
path: root/src/user/libfont
diff options
context:
space:
mode:
authorBenji Dial <benji6283@gmail.com>2021-02-17 16:35:02 -0500
committerBenji Dial <benji6283@gmail.com>2021-02-17 16:35:02 -0500
commit3a3602861226e995d95a8898668cd559c3ca1cf6 (patch)
tree0d5bc0494297a12e9ccb76b31cb365c843216735 /src/user/libfont
parent49d76d30a55707e2bf95fd9ba03296489fac8d1d (diff)
downloadportland-os-3a3602861226e995d95a8898668cd559c3ca1cf6.tar.gz
quick bitmap font format, borrowing new default font from X
Diffstat (limited to 'src/user/libfont')
-rw-r--r--src/user/libfont/bdf.c15
-rw-r--r--src/user/libfont/bdf.h5
-rw-r--r--src/user/libfont/fonts.c21
-rw-r--r--src/user/libfont/pbf.c43
-rw-r--r--src/user/libfont/pbf.h11
5 files changed, 71 insertions, 24 deletions
diff --git a/src/user/libfont/bdf.c b/src/user/libfont/bdf.c
index 2c19a15..de5489d 100644
--- a/src/user/libfont/bdf.c
+++ b/src/user/libfont/bdf.c
@@ -17,16 +17,10 @@ static inline uint8_t hex_to_n(char ch) {
}
//very minimal implementation
-bool try_load_bdf(const char *path, struct font_info *into) {
- struct file *f = open_file(path);
- if (!f)
- PANIC("Can't open font file sent by get_font.");
-
+bool try_load_bdf(struct file *f, struct font_info *into) {
read_line_from_file(f, line_buf, LINE_BUF_SIZE - 1);
- if (!strequ(line_buf, "STARTFONT 2.1")) {
- close_file(f);
+ if (!strequ(line_buf, "STARTFONT 2.1"))
return false;
- }
for (uint16_t i = 0; i < 256; ++i)
into->bitmaps[i] = 0;
@@ -71,9 +65,6 @@ bool try_load_bdf(const char *path, struct font_info *into) {
}
}
- close_file(f);
- f = 0;
-
if ((into->char_height == -1) ||
(into->char_width == -1) ||
(into->space_height == -1) ||
@@ -90,8 +81,6 @@ bool try_load_bdf(const char *path, struct font_info *into) {
return true;
bad_format:
- if (f)
- close_file(f);
for (uint16_t i = 0; i < 256; ++i)
if (into->bitmaps[i])
free_block(into->bitmaps[i]);
diff --git a/src/user/libfont/bdf.h b/src/user/libfont/bdf.h
index 4ed4189..cbf3772 100644
--- a/src/user/libfont/bdf.h
+++ b/src/user/libfont/bdf.h
@@ -1,8 +1,11 @@
#ifndef LIBFONT_BDF_H
#define LIBFONT_BDF_H
+#include <libfont/fonts.h>
+#include <knob/file.h>
+
#include <stdbool.h>
-bool try_load_bdf(const char *path, struct font_info *into);
+bool try_load_bdf(struct file *f, struct font_info *into);
#endif \ No newline at end of file
diff --git a/src/user/libfont/fonts.c b/src/user/libfont/fonts.c
index 1692992..c085604 100644
--- a/src/user/libfont/fonts.c
+++ b/src/user/libfont/fonts.c
@@ -6,14 +6,18 @@
#include "filist.h"
#include "bdf.h"
+#include "pbf.h"
#define FONT_PATH "fonts/"
#define FONT_PATH_L 6
struct font_loader_t {
const char *ext;
- bool (*func)(const char *, struct font_info *);
+ bool (*func)(struct file *f, struct font_info *);
} font_loaders[] = {
+ { .ext = ".pbf",
+ .func = try_load_pbf
+ },
{ .ext = ".bdf",
.func = try_load_bdf
},
@@ -36,20 +40,17 @@ struct font_info *get_font(const char *name) {
blockcpy(buf + FONT_PATH_L, name, name_len);
strcpy(buf + FONT_PATH_L + name_len, i->ext);
struct file *f = open_file(buf);
- if (!f) {
- free_block(buf);
+ free_block(buf);
+ if (!f)
continue;
- }
- syslogf("[libfont] Loading %s%s...", name, i->ext);
- if (i->func(buf, font)) {
+ //syslogf("[libfont] Loading %s%s...", name, i->ext);
+ if (i->func(f, font)) {
close_file(f);
- free_block(buf);
- syslogf("[libfont] Loaded %s%s.", name, i->ext);
+ //syslogf("[libfont] Loaded %s%s.", name, i->ext);
return font;
}
close_file(f);
- free_block(buf);
- syslogf("[libfont] Failed to load %s%s.", name, i->ext);
+ //syslogf("[libfont] Failed to load %s%s.", name, i->ext);
}
del_last();
return 0;
diff --git a/src/user/libfont/pbf.c b/src/user/libfont/pbf.c
new file mode 100644
index 0000000..34d5be5
--- /dev/null
+++ b/src/user/libfont/pbf.c
@@ -0,0 +1,43 @@
+#include <libfont/fonts.h>
+
+#include <knob/panic.h>
+#include <knob/file.h>
+#include <knob/heap.h>
+
+#include <stdbool.h>
+
+bool try_load_pbf(struct file *f, struct font_info *into) {
+ uint8_t head[4];
+ if (read_from_file(f, 4, head) != 4)
+ return false;
+
+ into->space_width = head[0] + head[2];
+ into->space_height = head[1] + head[3];
+ into->char_width = head[0];
+ into->char_height = head[1];
+
+ const uint16_t bm_size = head[0] * head[1];
+ const uint16_t bm_bytes = (bm_size - 1) / 8 + 1;
+
+ uint32_t bm_offsets[256];
+ if (read_from_file(f, 4 * 256, bm_offsets) != 4 * 256)
+ return false;
+
+ uint8_t bm_buf[256 * 256 / 8];
+
+ for (uint16_t i = 0; i < 256; ++i)
+ if (bm_offsets[i] == 0xffffffff)
+ into->bitmaps[i] = 0;
+ else {
+ bool *bp = get_block(bm_size);
+ if (!bp)
+ PANIC("couldn't allocate memory in pbf loader (todo: fail gracefully)");
+ seek_file_to(f, 4 + 4 * 256 + bm_offsets[i]);
+ read_from_file(f, bm_bytes, bm_buf);
+ for (uint16_t j = 0; j < bm_size; ++j)
+ bp[j] = (bm_buf[j / 8] >> (j % 8)) & 1;
+ into->bitmaps[i] = bp;
+ }
+
+ return true;
+} \ No newline at end of file
diff --git a/src/user/libfont/pbf.h b/src/user/libfont/pbf.h
new file mode 100644
index 0000000..bcf5d18
--- /dev/null
+++ b/src/user/libfont/pbf.h
@@ -0,0 +1,11 @@
+#ifndef LIBFONT_PBF_H
+#define LIBFONT_PBF_H
+
+#include <libfont/fonts.h>
+#include <knob/file.h>
+
+#include <stdbool.h>
+
+bool try_load_pbf(struct file *f, struct font_info *into);
+
+#endif \ No newline at end of file