[Teton] Load a font file and write a character to the screen.

This commit is contained in:
Drew Galbraith 2023-11-21 19:14:02 -08:00
parent 96063126cb
commit fe44804dd9
19 changed files with 412 additions and 17 deletions

View file

@ -1,6 +1,7 @@
#include "framebuffer/framebuffer.h"
Framebuffer::Framebuffer(const FramebufferInfo& info) : fb_info_(info) {
Framebuffer::Framebuffer(const FramebufferInfo& info)
: fb_info_(info), cursor_pos_(0) {
uint64_t buff_size_bytes = fb_info_.height() * fb_info_.pitch();
fb_memory_ = OwnedMemoryRegion::DirectPhysical(fb_info_.address_phys(),
buff_size_bytes);
@ -11,3 +12,18 @@ void Framebuffer::DrawPixel(uint32_t row, uint32_t col, uint32_t pixel) {
// Div by 4 because pitch is in bytes and fb_ is a 32bit array.
fb_[(row * fb_info_.pitch() / 4) + col] = pixel;
}
void Framebuffer::DrawGlyph(uint8_t* glyph) {
uint32_t gl_width = 8;
uint32_t gl_height = 16;
for (uint8_t r = 0; r < gl_height; r++) {
for (uint8_t c = 0; c < gl_width; c++) {
if (((glyph[r] >> c) % 2) == 1) {
DrawPixel(r, gl_width - c - 1, 0xFFFF'FFFF);
} else {
DrawPixel(r, gl_width - c - 1, 0);
}
}
}
};

View file

@ -9,6 +9,8 @@ class Framebuffer {
void DrawPixel(uint32_t row, uint32_t col, uint32_t pixel);
void DrawGlyph(uint8_t* glyph);
private:
// FIXME: Implement Yunq copy or move so we
// don't have to store a reference here.
@ -16,4 +18,5 @@ class Framebuffer {
OwnedMemoryRegion fb_memory_;
uint32_t* fb_;
uint32_t cursor_pos_;
};

View file

@ -0,0 +1,46 @@
#include "framebuffer/psf.h"
#include <glacier/memory/move.h>
#include <mammoth/debug.h>
namespace {
const uint32_t kMagic = 0x864AB572;
}
Psf::Psf(OwnedMemoryRegion&& psf_file)
: psf_file_(glcr::Move(psf_file)),
header_(reinterpret_cast<PsfHeader*>(psf_file_.vaddr())) {
EnsureValid();
}
void Psf::DumpHeader() {
dbgln("Magic: {x}", header_->magic);
dbgln("Version: {x}", header_->version);
dbgln("Header Sz: {x}", header_->headersize);
dbgln("Flags: {x}", header_->flags);
dbgln("Length: {x}", header_->numglyph);
dbgln("Glyph Size: {x}", header_->bytesperglyph);
dbgln("Height: {x}", header_->height);
dbgln("Width: {x}", header_->width);
}
void Psf::EnsureValid() {
if (header_->magic != kMagic) {
dbgln("PSF: Magic value: {x}", header_->magic);
crash("PSF: Invalid magic value", glcr::INVALID_ARGUMENT);
}
if (header_->version != 0) {
crash("PSF non-zero version", glcr::INVALID_ARGUMENT);
}
if (header_->height != 0x10) {
crash("PSF height other than 16 not handled", glcr::UNIMPLEMENTED);
}
if (header_->width != 0x8) {
crash("PSF width other than 8 not handled", glcr::UNIMPLEMENTED);
}
}

View file

@ -0,0 +1,34 @@
#pragma once
#include <mammoth/memory_region.h>
struct PsfHeader {
uint32_t magic; /* magic bytes to identify PSF */
uint32_t version; /* zero */
uint32_t headersize; /* offset of bitmaps in file, 32 */
uint32_t flags; /* 0 if there's no unicode table */
uint32_t numglyph; /* number of glyphs */
uint32_t bytesperglyph; /* size of each glyph */
uint32_t height; /* height in pixels */
uint32_t width; /* width in pixels */
};
class Psf {
public:
Psf(OwnedMemoryRegion&& psf_file);
void DumpHeader();
uint32_t size() { return header_->numglyph; }
uint8_t* glyph(uint32_t index) {
return reinterpret_cast<uint8_t*>(psf_file_.vaddr() + header_->headersize +
(index * header_->bytesperglyph));
}
private:
OwnedMemoryRegion psf_file_;
PsfHeader* header_;
void EnsureValid();
};