Kernel: Move font code to its own library LibFont

This commit is contained in:
Bananymous 2024-05-31 10:47:05 +03:00
parent ae3ae6fd0e
commit 0501f3bd99
11 changed files with 155 additions and 85 deletions

View File

@ -21,6 +21,7 @@ add_subdirectory(bootloader)
add_subdirectory(BAN)
add_subdirectory(libc)
add_subdirectory(LibELF)
add_subdirectory(LibFont)
add_subdirectory(LibGUI)
add_subdirectory(LibInput)
add_subdirectory(userspace)
@ -35,6 +36,7 @@ add_custom_target(headers
DEPENDS ban-headers
DEPENDS libc-headers
DEPENDS libelf-headers
DEPENDS libfont-headers
DEPENDS libgui-headers
DEPENDS libinput-headers
)
@ -45,6 +47,7 @@ add_custom_target(install-sysroot
DEPENDS libc-install
DEPENDS userspace-install
DEPENDS libelf-install
DEPENDS libfont-install
DEPENDS libgui-install
DEPENDS libinput-install
)

24
LibFont/CMakeLists.txt Normal file
View File

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.26)
project(libfont CXX)
set(LIBGUI_SOURCES
Font.cpp
)
add_custom_target(libfont-headers
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${BANAN_INCLUDE}/
DEPENDS sysroot
)
add_library(libfont ${LIBGUI_SOURCES})
add_dependencies(libfont headers libc-install)
target_link_libraries(libfont PUBLIC libc)
add_custom_target(libfont-install
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/libfont.a ${BANAN_LIB}/
DEPENDS libfont
BYPRODUCTS ${BANAN_LIB}/libfont.a
)
set(CMAKE_STATIC_LIBRARY_PREFIX "")

View File

@ -1,9 +1,13 @@
#include <BAN/Debug.h>
#include <BAN/Endianness.h>
#include <BAN/ScopeGuard.h>
#include <BAN/UTF8.h>
#include <kernel/Font.h>
#include <LibFont/Font.h>
#if __is_kernel
#include <kernel/FS/VirtualFileSystem.h>
#include <kernel/Process.h>
#endif
#include <fcntl.h>
@ -23,59 +27,86 @@
#define PSF2_STARTSEQ 0xFE
#define PSF2_SEPARATOR 0xFF
#if __is_kernel
extern uint8_t _binary_font_prefs_psf_start[];
extern uint8_t _binary_font_prefs_psf_end[];
#endif
namespace Kernel
namespace LibFont
{
#if __is_kernel
BAN::ErrorOr<Font> Font::prefs()
{
size_t font_data_size = _binary_font_prefs_psf_end - _binary_font_prefs_psf_start;
BAN::Span<const uint8_t> font_data(_binary_font_prefs_psf_start, font_data_size);
return parse_psf1(font_data);
return parse_psf1(BAN::ConstByteSpan(_binary_font_prefs_psf_start, font_data_size));
}
#endif
BAN::ErrorOr<Font> Font::load(BAN::StringView path)
{
auto inode = TRY(VirtualFileSystem::get().file_from_absolute_path({ 0, 0, 0, 0 }, path, O_RDONLY)).inode;
BAN::Vector<uint8_t> file_data;
TRY(file_data.resize(inode->size()));
TRY(inode->read(0, file_data.span()));
#if __is_kernel
auto inode = TRY(Kernel::VirtualFileSystem::get().file_from_absolute_path({ 0, 0, 0, 0 }, path, O_RDONLY)).inode;
TRY(file_data.resize(inode->size()));
TRY(inode->read(0, BAN::ByteSpan(file_data.span())));
#else
char path_buffer[PATH_MAX];
strncpy(path_buffer, path.data(), path.size());
path_buffer[path.size()] = '\0';
int fd = open(path_buffer, O_RDONLY);
if (fd == -1)
return BAN::Error::from_errno(errno);
BAN::ScopeGuard file_closer([fd] { close(fd); });
struct stat st;
if (fstat(fd, &st) == -1)
return BAN::Error::from_errno(errno);
TRY(file_data.resize(st.st_size));
ssize_t total_read = 0;
while (total_read < st.st_size)
{
ssize_t nread = read(fd, file_data.data() + total_read, st.st_size - total_read);
if (nread == -1)
return BAN::Error::from_errno(errno);
total_read += nread;
}
#endif
if (file_data.size() < 4)
return BAN::Error::from_error_code(ErrorCode::Font_FileTooSmall);
return BAN::Error::from_errno(EINVAL);
if (file_data[0] == PSF1_MAGIC0 && file_data[1] == PSF1_MAGIC1)
return TRY(parse_psf1(file_data.span()));
return TRY(parse_psf1(BAN::ConstByteSpan(file_data.span())));
if (file_data[0] == PSF2_MAGIC0 && file_data[1] == PSF2_MAGIC1 && file_data[2] == PSF2_MAGIC2 && file_data[3] == PSF2_MAGIC3)
return TRY(parse_psf2(file_data.span()));
return TRY(parse_psf2(BAN::ConstByteSpan(file_data.span())));
return BAN::Error::from_error_code(ErrorCode::Font_Unsupported);
return BAN::Error::from_errno(ENOTSUP);
}
BAN::ErrorOr<Font> Font::parse_psf1(BAN::Span<const uint8_t> font_data)
BAN::ErrorOr<Font> Font::parse_psf1(BAN::ConstByteSpan font_data)
{
if (font_data.size() < 4)
return BAN::Error::from_error_code(ErrorCode::Font_FileTooSmall);
struct PSF1Header
{
uint8_t magic[2];
uint8_t mode;
uint8_t char_size;
};
const PSF1Header& header = *(const PSF1Header*)font_data.data();
if (font_data.size() < sizeof(PSF1Header))
return BAN::Error::from_errno(EINVAL);
const auto& header = font_data.as<const PSF1Header>();
uint32_t glyph_count = header.mode & PSF1_MODE512 ? 512 : 256;
uint32_t glyph_size = header.char_size;
uint32_t glyph_data_size = glyph_size * glyph_count;
if (font_data.size() < sizeof(PSF1Header) + glyph_data_size)
return BAN::Error::from_error_code(ErrorCode::Font_FileTooSmall);
return BAN::Error::from_errno(EINVAL);
BAN::Vector<uint8_t> glyph_data;
TRY(glyph_data.resize(glyph_data_size));
@ -125,7 +156,7 @@ namespace Kernel
}
if (codepoint_redef)
dwarnln("Font contsins multiple definitions for same codepoint(s)");
dwarnln("Font contains multiple definitions for same codepoint(s)");
if (codepoint_sequence)
dwarnln("Font contains codepoint sequences (not supported)");
@ -138,7 +169,7 @@ namespace Kernel
return result;
}
BAN::ErrorOr<Font> Font::parse_psf2(BAN::Span<const uint8_t> font_data)
BAN::ErrorOr<Font> Font::parse_psf2(BAN::ConstByteSpan font_data)
{
struct PSF2Header
{
@ -153,14 +184,13 @@ namespace Kernel
};
if (font_data.size() < sizeof(PSF2Header))
return BAN::Error::from_error_code(ErrorCode::Font_FileTooSmall);
const PSF2Header& header = *(const PSF2Header*)font_data.data();
return BAN::Error::from_errno(EINVAL);
const auto& header = font_data.as<const PSF2Header>();
uint32_t glyph_data_size = header.glyph_count * header.glyph_size;
if (font_data.size() < glyph_data_size + header.header_size)
return BAN::Error::from_error_code(ErrorCode::Font_FileTooSmall);
return BAN::Error::from_errno(EINVAL);
BAN::Vector<uint8_t> glyph_data;
TRY(glyph_data.resize(glyph_data_size));

View File

@ -1,17 +1,19 @@
#pragma once
#include <BAN/ByteSpan.h>
#include <BAN/HashMap.h>
#include <BAN/Span.h>
#include <BAN/StringView.h>
namespace Kernel
namespace LibFont
{
class Font
{
public:
static BAN::ErrorOr<Font> load(BAN::StringView);
static BAN::ErrorOr<Font> load(BAN::StringView path);
#if __is_kernel
static BAN::ErrorOr<Font> prefs();
#endif
uint32_t width() const { return m_width; }
uint32_t height() const { return m_height; }
@ -21,8 +23,8 @@ namespace Kernel
const uint8_t* glyph(uint32_t) const;
private:
static BAN::ErrorOr<Font> parse_psf1(BAN::Span<const uint8_t>);
static BAN::ErrorOr<Font> parse_psf2(BAN::Span<const uint8_t>);
static BAN::ErrorOr<Font> parse_psf1(BAN::ConstByteSpan);
static BAN::ErrorOr<Font> parse_psf2(BAN::ConstByteSpan);
private:
BAN::HashMap<uint32_t, uint32_t> m_glyph_offsets;

View File

@ -29,7 +29,6 @@ set(KERNEL_SOURCES
kernel/Device/NullDevice.cpp
kernel/Device/ZeroDevice.cpp
kernel/Errors.cpp
kernel/Font.cpp
kernel/FS/DevFS/FileSystem.cpp
kernel/FS/Ext2/FileSystem.cpp
kernel/FS/Ext2/Inode.cpp
@ -149,6 +148,10 @@ set(LIBELF_SOURCES
../LibELF/LibELF/LoadableELF.cpp
)
set(LIBFONT_SOURCES
../LibFont/Font.cpp
)
set(LIBINPUT_SOURCE
../LibInput/KeyboardLayout.cpp
../LibInput/KeyEvent.cpp
@ -160,6 +163,7 @@ set(KERNEL_SOURCES
${BAN_SOURCES}
${KLIBC_SOURCES}
${LIBELF_SOURCES}
${LIBFONT_SOURCES}
${LIBINPUT_SOURCE}
)

View File

@ -16,7 +16,7 @@ namespace Kernel
public:
void set_termios(const termios& termios) { m_termios = termios; }
termios get_termios() const { return m_termios; }
virtual void set_font(const Font&) {};
virtual void set_font(const LibFont::Font&) {};
void set_foreground_pgrp(pid_t pgrp) { m_foreground_pgrp = pgrp; }
pid_t foreground_pgrp() const { return m_foreground_pgrp; }

View File

@ -1,9 +1,12 @@
#pragma once
#include <kernel/Font.h>
#include <LibFont/Font.h>
#include <stdint.h>
namespace Kernel
{
class TerminalDriver
{
public:
@ -22,7 +25,7 @@ public:
};
public:
TerminalDriver() : m_font(MUST(Kernel::Font::prefs())) {}
TerminalDriver() : m_font(MUST(LibFont::Font::prefs())) {}
virtual ~TerminalDriver() {}
virtual uint32_t width() const = 0;
virtual uint32_t height() const = 0;
@ -32,11 +35,11 @@ public:
virtual void set_cursor_position(uint32_t, uint32_t) = 0;
void set_font(const Kernel::Font& font) { m_font = font; };
const Kernel::Font& font() const { return m_font; }
void set_font(const LibFont::Font& font) { m_font = font; };
const LibFont::Font& font() const { return m_font; }
private:
Kernel::Font m_font;
LibFont::Font m_font;
};
namespace TerminalColor
@ -59,3 +62,5 @@ namespace TerminalColor
static constexpr TerminalDriver::Color BRIGHT_YELLOW = 0xFFFF55;
static constexpr TerminalDriver::Color BRIGHT_WHITE = 0xFFFFFF;
}
}

View File

@ -15,7 +15,7 @@ namespace Kernel
public:
static BAN::ErrorOr<BAN::RefPtr<VirtualTTY>> create(TerminalDriver*);
virtual void set_font(const Font&) override;
virtual void set_font(const LibFont::Font&) override;
virtual uint32_t height() const override { return m_height; }
virtual uint32_t width() const override { return m_width; }

View File

@ -8,7 +8,7 @@
#include <ctype.h>
extern TerminalDriver* g_terminal_driver;
extern Kernel::TerminalDriver* g_terminal_driver;
namespace Debug
{
@ -68,6 +68,8 @@ namespace Debug
void putchar(char ch)
{
using namespace Kernel;
if (Kernel::Serial::has_devices())
return Kernel::Serial::putchar_any(ch);
if (Kernel::TTY::is_initialized())

View File

@ -62,7 +62,7 @@ namespace Kernel
m_terminal_driver->clear(m_background);
}
void VirtualTTY::set_font(const Kernel::Font& font)
void VirtualTTY::set_font(const LibFont::Font& font)
{
m_terminal_driver->set_font(font);

View File

@ -77,7 +77,7 @@ static void parse_command_line()
}
}
TerminalDriver* g_terminal_driver = nullptr;
Kernel::TerminalDriver* g_terminal_driver = nullptr;
static void init2(void*);