#define MODULE "text" #include "render/TextRenderer.h" #include "bootboot.h" #include "init/InitRD.h" #include "io/Serial.h" #include "log/Log.h" #include "psf1.h" #include "render/Framebuffer.h" #include "std/stdio.h" #include "std/string.h" extern BOOTBOOT bootboot; static psf1_t* font; static Color bgColor = Color::Black; static Color fgColor = Color::White; static uint32_t xpos = 0; static uint32_t ypos = 0; #define FONT_HEIGHT font->charsize #define FONT_WIDTH 8 bool TextRenderer::try_initialize() { InitRD::File font_file = InitRD::open("boot/font.psf"); if (!font_file.addr) { kerrorln("Failed to load boot/font.psf from initrd"); return false; } font = (psf1_t*)font_file.addr; if (font->magic[0] != psf1_magic[0] || font->magic[1] != psf1_magic[1]) { kerrorln("Font magic does not match PSF font magic: 0x%x 0x%x", font->magic[0], font->magic[1]); return false; } kdbgln("Loaded font %s, height %d, address %lx, mode %d", font_file.name, font->charsize, (uintptr_t)&font->glyphs[0], font->mode); return true; } bool TextRenderer::is_initialized() { return font; // if font is NULL, not initialized, else yes } static void putchar_at_offset(char c, uint32_t cx, uint32_t cy, Color& fg, Color& bg) { volatile uint8_t* glyph = (uint8_t*)font->glyphs + (c * font->charsize); for (uint32_t y = 0; y < FONT_HEIGHT; y++) { for (uint32_t x = 0; x < FONT_WIDTH; x++) { if (((*glyph & (0b10000000 >> x)) > 0)) { framebuffer0.set_pixel(cx + x, cy + y, fg); } else { framebuffer0.set_pixel(cx + x, cy + y, bg); } } glyph++; } } void TextRenderer::putchar(char chr) { switch (chr) { case '\n': ypos += FONT_HEIGHT; if ((ypos + FONT_HEIGHT) >= bootboot.fb_height) { memcpy((void*)bootboot.fb_ptr, (uint32_t*)bootboot.fb_ptr + (bootboot.fb_scanline * FONT_HEIGHT), bootboot.fb_size - (sizeof(uint32_t) * bootboot.fb_scanline * FONT_HEIGHT)); ypos -= FONT_HEIGHT; framebuffer0.paint_rect(0, ypos, bootboot.fb_width, FONT_HEIGHT, Color::Black); } xpos = 0; break; case '\r': xpos = 0; break; case '\b': if (xpos != 0) { xpos -= FONT_WIDTH; framebuffer0.paint_rect(xpos, ypos, FONT_WIDTH, FONT_HEIGHT, Color::Black); } break; default: putchar_at_offset(chr, xpos, ypos, fgColor, bgColor); xpos += FONT_WIDTH; if ((xpos + FONT_WIDTH) > bootboot.fb_width) { xpos = 0; ypos += FONT_HEIGHT; if (ypos > bootboot.fb_height) { memcpy((void*)bootboot.fb_ptr, (uint32_t*)bootboot.fb_ptr + (bootboot.fb_scanline * FONT_HEIGHT), bootboot.fb_size - (sizeof(uint32_t) * bootboot.fb_scanline * FONT_HEIGHT)); ypos -= FONT_HEIGHT; framebuffer0.paint_rect(0, ypos, bootboot.fb_width, FONT_HEIGHT, Color::Black); } } break; } } void TextRenderer::write(const char* str, size_t size) { for (size_t i = 0; i < size; i++) { putchar(str[i]); } }