diff options
author | cflip <cflip@cflip.net> | 2023-01-19 10:47:49 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-19 13:08:18 -0700 |
commit | d61a3f4921e55a753d4bc94de50dd7faffc3a348 (patch) | |
tree | 62e1393ff2ece4394f83c6fc7c339126d3e2a635 | |
parent | 7b3c0c1530fe0cafa8e0937833fa925086253c87 (diff) |
Load gzipped fonts from /usr/local/share/consolefonts/
The editor now loads fonts from /usr/ instead of the current working
directory. Since they are gzipped the font loading code now uses zlib to
read from the files.
These fonts in particular were installed with `make install-psf` from
the Terminus makefile, however the location may vary from system to
system.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | font.c | 43 | ||||
-rw-r--r-- | font.h | 3 | ||||
-rw-r--r-- | window.c | 2 |
4 files changed, 25 insertions, 25 deletions
@@ -1,5 +1,5 @@ CFLAGS=-std=c99 -g -Wall -LFLAGS=-lSDL2 +LFLAGS=-lSDL2 -lz OUT=editor OBJS=main.o \ @@ -4,6 +4,10 @@ #include <limits.h> #include <stdlib.h> +#include <zlib.h> + +#include "error.h" + /* Create a texture atlas containing all of the glyphs in a font. */ SDL_Texture *font_create_texture(SDL_Renderer *renderer, PSFFont *font) { @@ -45,46 +49,39 @@ PSFFont font_load(const char *filename) { PSFFont font; - FILE *fp = fopen(filename, "rb"); - if (!fp) + gzFile file = gzopen(filename, "rb"); + if (!file) fatal_error("Failed to open font from '%s'\n", filename); - fseek(fp, 0, SEEK_END); - size_t filesize = ftell(fp); - fseek(fp, 0, SEEK_SET); - - fread(&font.magic, 4, 1, fp); + gzread(file, &font.magic, 4); if (font.magic != PSF_MAGIC_NUMBER) fatal_error("Font header mismatch! '%s' has magic value %x.\n", filename, font.magic); - fread(&font.version, 4, 1, fp); - fread(&font.header_size, 4, 1, fp); - fread(&font.flags, 4, 1, fp); - fread(&font.num_glyphs, 4, 1, fp); - fread(&font.bytes_per_glyph, 4, 1, fp); - fread(&font.height, 4, 1, fp); - fread(&font.width, 4, 1, fp); + gzread(file, &font.version, 4); + gzread(file, &font.header_size, 4); + gzread(file, &font.flags, 4); + gzread(file, &font.num_glyphs, 4); + gzread(file, &font.bytes_per_glyph, 4); + gzread(file, &font.height, 4); + gzread(file, &font.width, 4); size_t glyph_buffer_size = font.num_glyphs * font.bytes_per_glyph; font.glyph_data = malloc(glyph_buffer_size); - fseek(fp, font.header_size, SEEK_SET); - fread(font.glyph_data, font.bytes_per_glyph, font.num_glyphs, fp); + gzseek(file, font.header_size, SEEK_SET); + gzread(file, font.glyph_data, font.bytes_per_glyph * font.num_glyphs); font.unicode_desc = NULL; if (font.flags == PSF_FLAG_UNICODE) { - size_t current_pos = ftell(fp); - size_t bytes_left = filesize - current_pos; - /* Store the file's unicode information in a buffer. */ - char *desc = malloc(bytes_left); - fread(desc, bytes_left, 1, fp); + char *desc = malloc(UNICODE_TABLE_SIZE); + gzread(file, desc, UNICODE_TABLE_SIZE); /* Create a buffer in our object to map character codes to glyphs */ font.unicode_desc = calloc(USHRT_MAX, 2); int glyph_index = 0; unsigned char letter = 0; - for (int i = 0; i < bytes_left; i++) { + for (int i = 0; i < UNICODE_TABLE_SIZE; i++) { unsigned char uc = desc[i]; if (uc == 0xff) { font.unicode_desc[letter] = glyph_index; @@ -95,7 +92,7 @@ PSFFont font_load(const char *filename) } } - fclose(fp); + gzclose(file); printf("Loaded a font with %d glyphs of size %dx%d (%d bytes per glyph)\n", font.num_glyphs, font.width, font.height, font.bytes_per_glyph); @@ -6,6 +6,9 @@ #define PSF_MAGIC_NUMBER 0x864ab572 #define PSF_FLAG_UNICODE 1 +/* TODO: It would be nice if this could be calculated while loading the font */ +#define UNICODE_TABLE_SIZE 1024 + typedef struct { uint32_t magic; uint32_t version; @@ -24,7 +24,7 @@ void window_init(const char *title, int rows, int cols) if (SDL_Init(SDL_INIT_VIDEO) != 0) fatal_error("Failed to init SDL: %s\n", SDL_GetError()); - font = font_load("terminus/ter-u24n.psf"); + font = font_load("/usr/local/share/consolefonts/ter-u18n.psf.gz"); window_width = cols * font.width; window_height = rows * font.height; |