From cefc4a76ca3efe74d116b4f5bec0b8350ac581e1 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Fri, 20 Aug 2021 19:25:42 +0200 Subject: [PATCH] gterm: Implement TERMINAL_FONT_SPACING and default it to 1 --- CONFIG.md | 4 +++- stage23/lib/gterm.c | 31 ++++++++++++++++++++++++++----- stage23/menu.c | 2 ++ test/limine.cfg | 2 -- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 933fc92c..7c34ca85 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -58,10 +58,12 @@ Some keys take *URIs* as values; these are described in the next section. * `MENU_BRANDING` - A string that will be displayed on top of the Limine menu. * `MENU_FONT` - URI path to a font file to be used instead of the default one for the menu and terminal. The font file must be a code page 437 character set comprised of 256 consecutive glyph bitmaps. Each glyph's bitmap must be expressed left to right (1 byte per row), and top to bottom (16 bytes per whole glyph by default; see `MENU_FONT_SIZE`). See e.g. the [VGA text mode font collection](https://github.com/viler-int10h/vga-text-mode-fonts) for fonts. * `TERMINAL_FONT` - Alias of `MENU_FONT`. -* `MENU_FONT_SIZE` - The size of the font in dots, which must correspond to the font file or the display will be garbled. Note that glyphs are always one byte wide, and columns over 8 are empty. Many fonts may be used in both 8- and 9-dot wide variants. Defaults to `8x16`. +* `MENU_FONT_SIZE` - The size of the font in dots, which must correspond to the font file or the display will be garbled. Note that glyphs are always one byte wide, and columns over 8 are empty. Many fonts may be used in both 8- and 9-dot wide variants. Defaults to `8x16`. Ignored if `MENU_FONT` or `TERMINAL_FONT` not set or if the font fails to load. * `TERMINAL_FONT_SIZE` - Alias of `MENU_FONT_SIZE`. * `MENU_FONT_SCALE` - Scaling for the font in the x and y directions. `2x2` would display the font in double size, which is useful on high-DPI displays at native resolution. `2x1` only makes the font twice as wide, similar to the VGA 40 column mode. `4x2` might be good for a narrow font on a high resolution display. Values over 8 are disallowed. Default is no scaling, i.e. `1x1`. * `TERMINAL_FONT_SCALE` - Alias of `MENU_FONT_SCALE`. +* `MENU_FONT_SPACING` - Horizontal spacing, in pixels, between glyphs on screen. It is equivalent to setting a font width of `+`, except this value is preserved even in case font loading fails, and it also applies to the built-in Limine font. Defaults to 1. 0 is allowed. +* `TERMINAL_FONT_SPACING` - Alias of `MENU_FONT_SPACING`. * `THEME_COLOURS` - Specifies the colour palette used by the terminal (AARRGGBB). It is a `;` separated array of 10 colours: black, red, green, brown, blue, magenta, cyan, gray, background, and foreground respectively. While an alpha transparency value can be specified for every colour, it is ignored for all but background. Ignored if `GRAPHICS` is not `yes`. * `THEME_COLORS` - Alias of `THEME_COLOURS`. * `THEME_BACKGROUND` - Alias of the background value in `THEME_COLOURS`. diff --git a/stage23/lib/gterm.c b/stage23/lib/gterm.c index 562cf450..86a9b2ec 100644 --- a/stage23/lib/gterm.c +++ b/stage23/lib/gterm.c @@ -16,8 +16,11 @@ #define VGA_FONT_MAX 16384 #define VGA_FONT_GLYPHS 256 -static size_t vga_font_width = 8; -static size_t vga_font_height = 16; +#define DEFAULT_FONT_WIDTH 8 +#define DEFAULT_FONT_HEIGHT 16 + +static size_t vga_font_width; +static size_t vga_font_height; static size_t glyph_width = 8; static size_t glyph_height = 16; @@ -658,15 +661,19 @@ bool gterm_init(size_t *_rows, size_t *_cols, size_t width, size_t height) { gterm_bpp = fbinfo.framebuffer_bpp; gterm_pitch = fbinfo.framebuffer_pitch; + vga_font_width = DEFAULT_FONT_WIDTH, vga_font_height = DEFAULT_FONT_HEIGHT; + + size_t font_width = DEFAULT_FONT_WIDTH, font_height = DEFAULT_FONT_HEIGHT; + char *menu_font_size = config_get_value(NULL, 0, "MENU_FONT_SIZE"); if (menu_font_size == NULL) { menu_font_size = config_get_value(NULL, 0, "TERMINAL_FONT_SIZE"); } if (menu_font_size != NULL) { - parse_resolution(&vga_font_width, &vga_font_height, NULL, menu_font_size); + parse_resolution(&font_width, &font_height, NULL, menu_font_size); } - size_t font_bytes = (vga_font_width * vga_font_height * VGA_FONT_GLYPHS) / 8; + size_t font_bytes = (font_width * font_height * VGA_FONT_GLYPHS) / 8; if (vga_font_bits == NULL) { vga_font_bits = ext_mem_alloc(VGA_FONT_MAX); @@ -689,10 +696,24 @@ bool gterm_init(size_t *_rows, size_t *_cols, size_t width, size_t height) { if (!uri_open(&f, menu_font)) { print("menu: Could not open font file.\n"); } else { - fread(&f, vga_font_bits, 0, font_bytes); + if (fread(&f, vga_font_bits, 0, font_bytes) == 0) { + vga_font_width = font_width; + vga_font_height = font_height; + } } } + size_t font_spacing = 1; + char *font_spacing_str = config_get_value(NULL, 0, "MENU_FONT_SPACING"); + if (font_spacing_str == NULL) { + font_spacing_str = config_get_value(NULL, 0, "TERMINAL_FONT_SPACING"); + } + if (font_spacing_str != NULL) { + font_spacing = strtoui(font_spacing_str, NULL, 10); + } + + vga_font_width += font_spacing; + // if not loaded (stage2) or custom font (stage3), load font if (vga_font_bool == NULL || menu_font != NULL) { vga_font_bool = ext_mem_alloc(VGA_FONT_GLYPHS * vga_font_height * vga_font_width * sizeof(bool)); diff --git a/stage23/menu.c b/stage23/menu.c index 7b83de5f..b66697da 100644 --- a/stage23/menu.c +++ b/stage23/menu.c @@ -81,9 +81,11 @@ static const char *VALID_KEYS[] = { "MENU_FONT", "MENU_FONT_SIZE", "MENU_FONT_SCALE", + "MENU_FONT_SPACING", "TERMINAL_FONT", "TERMINAL_FONT_SIZE", "TERMINAL_FONT_SCALE", + "TERMINAL_FONT_SPACING", "THEME_COLOURS", "THEME_COLORS", "THEME_BRIGHT_COLOURS", diff --git a/test/limine.cfg b/test/limine.cfg index 8221fbd4..8ae4363b 100644 --- a/test/limine.cfg +++ b/test/limine.cfg @@ -3,8 +3,6 @@ TIMEOUT=3 GRAPHICS=yes MENU_FONT=boot:///boot/font.bin VERBOSE=yes -TERMINAL_FONT_SIZE=9x16 -TERMINAL_FONT_SCALE=1x1 THEME_BACKGROUND=50000000