2023-03-17 21:16:22 +02:00
|
|
|
#include <BAN/ScopeGuard.h>
|
2024-05-31 10:47:05 +03:00
|
|
|
|
|
|
|
#include <LibFont/Font.h>
|
2024-06-06 16:45:01 +03:00
|
|
|
#include <LibFont/PSF.h>
|
2024-05-31 10:47:05 +03:00
|
|
|
|
|
|
|
#if __is_kernel
|
2023-09-25 22:07:12 +03:00
|
|
|
#include <kernel/FS/VirtualFileSystem.h>
|
2024-05-31 10:47:05 +03:00
|
|
|
#endif
|
2023-03-17 21:16:22 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
#if __is_kernel
|
2023-03-20 14:59:30 +02:00
|
|
|
extern uint8_t _binary_font_prefs_psf_start[];
|
|
|
|
extern uint8_t _binary_font_prefs_psf_end[];
|
2024-05-31 10:47:05 +03:00
|
|
|
#endif
|
2023-02-23 01:22:50 +02:00
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
namespace LibFont
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
#if __is_kernel
|
2023-02-23 01:22:50 +02:00
|
|
|
BAN::ErrorOr<Font> Font::prefs()
|
|
|
|
{
|
2023-03-20 14:59:30 +02:00
|
|
|
size_t font_data_size = _binary_font_prefs_psf_end - _binary_font_prefs_psf_start;
|
2024-06-06 16:45:01 +03:00
|
|
|
return load(BAN::ConstByteSpan(_binary_font_prefs_psf_start, font_data_size));
|
2023-02-23 01:22:50 +02:00
|
|
|
}
|
2024-05-31 10:47:05 +03:00
|
|
|
#endif
|
2023-02-23 01:22:50 +02:00
|
|
|
|
2023-02-22 22:28:12 +02:00
|
|
|
BAN::ErrorOr<Font> Font::load(BAN::StringView path)
|
|
|
|
{
|
2023-03-17 21:16:22 +02:00
|
|
|
BAN::Vector<uint8_t> file_data;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
#if __is_kernel
|
|
|
|
{
|
2024-06-06 16:45:01 +03:00
|
|
|
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())));
|
2024-05-31 10:47:05 +03:00
|
|
|
}
|
2024-06-06 16:45:01 +03:00
|
|
|
#else
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
2024-06-06 16:45:01 +03:00
|
|
|
char path_buffer[PATH_MAX];
|
|
|
|
strncpy(path_buffer, path.data(), path.size());
|
|
|
|
path_buffer[path.size()] = '\0';
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2024-06-06 16:45:01 +03:00
|
|
|
int fd = open(path_buffer, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
return BAN::Error::from_errno(errno);
|
|
|
|
BAN::ScopeGuard file_closer([fd] { close(fd); });
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2024-06-06 16:45:01 +03:00
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) == -1)
|
|
|
|
return BAN::Error::from_errno(errno);
|
|
|
|
TRY(file_data.resize(st.st_size));
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2024-06-06 16:45:01 +03:00
|
|
|
ssize_t total_read = 0;
|
|
|
|
while (total_read < st.st_size)
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
2024-06-06 16:45:01 +03:00
|
|
|
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;
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-06 16:45:01 +03:00
|
|
|
#endif
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2024-06-06 16:45:01 +03:00
|
|
|
return load(BAN::ConstByteSpan(file_data.span()));
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 16:45:01 +03:00
|
|
|
BAN::ErrorOr<Font> Font::load(BAN::ConstByteSpan font_data)
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
2024-06-06 16:45:01 +03:00
|
|
|
if (is_psf1(font_data))
|
|
|
|
return TRY(parse_psf1(font_data));
|
|
|
|
if (is_psf2(font_data))
|
|
|
|
return TRY(parse_psf2(font_data));
|
|
|
|
return BAN::Error::from_errno(ENOTSUP);
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|