2024-05-31 10:47:05 +03:00
|
|
|
#include <BAN/Debug.h>
|
2023-03-20 19:46:37 +02:00
|
|
|
#include <BAN/Endianness.h>
|
2023-03-17 21:16:22 +02:00
|
|
|
#include <BAN/ScopeGuard.h>
|
2023-03-20 13:35:54 +02:00
|
|
|
#include <BAN/UTF8.h>
|
2024-05-31 10:47:05 +03:00
|
|
|
|
|
|
|
#include <LibFont/Font.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
2023-03-20 19:46:37 +02:00
|
|
|
#define PSF1_MAGIC0 0x36
|
|
|
|
#define PSF1_MAGIC1 0x04
|
|
|
|
#define PSF1_MODE512 0x01
|
|
|
|
#define PSF1_MODEHASTAB 0x02
|
|
|
|
#define PSF1_MODEHASSEQ 0x04
|
|
|
|
#define PSF1_STARTSEQ 0xFFFE
|
|
|
|
#define PSF1_SEPARATOR 0xFFFF
|
|
|
|
|
|
|
|
#define PSF2_MAGIC0 0x72
|
|
|
|
#define PSF2_MAGIC1 0xB5
|
|
|
|
#define PSF2_MAGIC2 0x4A
|
|
|
|
#define PSF2_MAGIC3 0x86
|
|
|
|
#define PSF2_HAS_UNICODE_TABLE 0x01
|
|
|
|
#define PSF2_STARTSEQ 0xFE
|
|
|
|
#define PSF2_SEPARATOR 0xFF
|
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-05-31 10:47:05 +03:00
|
|
|
return parse_psf1(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
|
|
|
|
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
|
2023-02-22 22:28:12 +02:00
|
|
|
|
|
|
|
if (file_data.size() < 4)
|
2024-05-31 10:47:05 +03:00
|
|
|
return BAN::Error::from_errno(EINVAL);
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2023-03-20 19:46:37 +02:00
|
|
|
if (file_data[0] == PSF1_MAGIC0 && file_data[1] == PSF1_MAGIC1)
|
2024-05-31 10:47:05 +03:00
|
|
|
return TRY(parse_psf1(BAN::ConstByteSpan(file_data.span())));
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2023-03-20 19:46:37 +02:00
|
|
|
if (file_data[0] == PSF2_MAGIC0 && file_data[1] == PSF2_MAGIC1 && file_data[2] == PSF2_MAGIC2 && file_data[3] == PSF2_MAGIC3)
|
2024-05-31 10:47:05 +03:00
|
|
|
return TRY(parse_psf2(BAN::ConstByteSpan(file_data.span())));
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
return BAN::Error::from_errno(ENOTSUP);
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
BAN::ErrorOr<Font> Font::parse_psf1(BAN::ConstByteSpan font_data)
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
|
|
|
struct PSF1Header
|
|
|
|
{
|
2023-03-20 19:46:37 +02:00
|
|
|
uint8_t magic[2];
|
2023-02-22 22:28:12 +02:00
|
|
|
uint8_t mode;
|
|
|
|
uint8_t char_size;
|
|
|
|
};
|
2024-05-31 10:47:05 +03:00
|
|
|
|
|
|
|
if (font_data.size() < sizeof(PSF1Header))
|
|
|
|
return BAN::Error::from_errno(EINVAL);
|
|
|
|
const auto& header = font_data.as<const PSF1Header>();
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-03-20 19:46:37 +02:00
|
|
|
uint32_t glyph_count = header.mode & PSF1_MODE512 ? 512 : 256;
|
|
|
|
uint32_t glyph_size = header.char_size;
|
2023-02-22 22:28:12 +02:00
|
|
|
uint32_t glyph_data_size = glyph_size * glyph_count;
|
|
|
|
|
|
|
|
if (font_data.size() < sizeof(PSF1Header) + glyph_data_size)
|
2024-05-31 10:47:05 +03:00
|
|
|
return BAN::Error::from_errno(EINVAL);
|
2023-02-22 22:28:12 +02:00
|
|
|
|
|
|
|
BAN::Vector<uint8_t> glyph_data;
|
|
|
|
TRY(glyph_data.resize(glyph_data_size));
|
|
|
|
memcpy(glyph_data.data(), font_data.data() + sizeof(PSF1Header), glyph_data_size);
|
|
|
|
|
2023-03-20 14:52:42 +02:00
|
|
|
BAN::HashMap<uint32_t, uint32_t> glyph_offsets;
|
2023-02-22 22:28:12 +02:00
|
|
|
TRY(glyph_offsets.reserve(glyph_count));
|
|
|
|
|
|
|
|
bool codepoint_redef = false;
|
2023-03-20 14:52:42 +02:00
|
|
|
bool codepoint_sequence = false;
|
2023-02-22 22:28:12 +02:00
|
|
|
|
2023-03-20 19:46:37 +02:00
|
|
|
if (header.mode & (PSF1_MODEHASTAB | PSF1_MODEHASSEQ))
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
|
|
|
uint32_t current_index = sizeof(PSF1Header) + glyph_data_size;
|
|
|
|
|
|
|
|
uint32_t glyph_index = 0;
|
|
|
|
while (current_index < font_data.size())
|
|
|
|
{
|
|
|
|
uint16_t lo = font_data[current_index];
|
|
|
|
uint16_t hi = font_data[current_index + 1];
|
|
|
|
uint16_t codepoint = (hi << 8) | lo;
|
|
|
|
|
2023-03-20 19:46:37 +02:00
|
|
|
if (codepoint == PSF1_STARTSEQ)
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
2023-03-20 14:52:42 +02:00
|
|
|
codepoint_sequence = true;
|
|
|
|
break;
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
2023-03-20 19:46:37 +02:00
|
|
|
else if (codepoint == PSF1_SEPARATOR)
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
2023-03-20 14:52:42 +02:00
|
|
|
glyph_index++;
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
2023-03-20 14:52:42 +02:00
|
|
|
else
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
|
|
|
if (glyph_offsets.contains(codepoint))
|
|
|
|
codepoint_redef = true;
|
|
|
|
else
|
|
|
|
TRY(glyph_offsets.insert(codepoint, glyph_index * glyph_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
current_index += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < glyph_count; i++)
|
|
|
|
TRY(glyph_offsets.insert(i, i * glyph_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codepoint_redef)
|
2024-05-31 10:47:05 +03:00
|
|
|
dwarnln("Font contains multiple definitions for same codepoint(s)");
|
2023-03-20 14:52:42 +02:00
|
|
|
if (codepoint_sequence)
|
|
|
|
dwarnln("Font contains codepoint sequences (not supported)");
|
2023-02-22 22:28:12 +02:00
|
|
|
|
|
|
|
Font result;
|
|
|
|
result.m_glyph_offsets = BAN::move(glyph_offsets);
|
|
|
|
result.m_glyph_data = BAN::move(glyph_data);
|
|
|
|
result.m_width = 8;
|
2023-03-20 19:46:37 +02:00
|
|
|
result.m_height = header.char_size;
|
2023-02-22 22:28:12 +02:00
|
|
|
result.m_pitch = 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
BAN::ErrorOr<Font> Font::parse_psf2(BAN::ConstByteSpan font_data)
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
|
|
|
struct PSF2Header
|
|
|
|
{
|
2023-03-20 19:46:37 +02:00
|
|
|
uint8_t magic[4];
|
|
|
|
BAN::LittleEndian<uint32_t> version;
|
|
|
|
BAN::LittleEndian<uint32_t> header_size;
|
|
|
|
BAN::LittleEndian<uint32_t> flags;
|
|
|
|
BAN::LittleEndian<uint32_t> glyph_count;
|
|
|
|
BAN::LittleEndian<uint32_t> glyph_size;
|
|
|
|
BAN::LittleEndian<uint32_t> height;
|
|
|
|
BAN::LittleEndian<uint32_t> width;
|
2023-02-22 22:28:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (font_data.size() < sizeof(PSF2Header))
|
2024-05-31 10:47:05 +03:00
|
|
|
return BAN::Error::from_errno(EINVAL);
|
|
|
|
const auto& header = font_data.as<const PSF2Header>();
|
2023-02-22 22:28:12 +02:00
|
|
|
|
|
|
|
uint32_t glyph_data_size = header.glyph_count * header.glyph_size;
|
|
|
|
|
|
|
|
if (font_data.size() < glyph_data_size + header.header_size)
|
2024-05-31 10:47:05 +03:00
|
|
|
return BAN::Error::from_errno(EINVAL);
|
2023-02-22 22:28:12 +02:00
|
|
|
|
|
|
|
BAN::Vector<uint8_t> glyph_data;
|
|
|
|
TRY(glyph_data.resize(glyph_data_size));
|
|
|
|
memcpy(glyph_data.data(), font_data.data() + header.header_size, glyph_data_size);
|
|
|
|
|
2023-03-20 14:52:42 +02:00
|
|
|
BAN::HashMap<uint32_t, uint32_t> glyph_offsets;
|
2023-02-22 22:28:12 +02:00
|
|
|
TRY(glyph_offsets.reserve(400));
|
|
|
|
|
2023-03-20 14:52:42 +02:00
|
|
|
bool invalid_utf = false;
|
2023-02-22 22:28:12 +02:00
|
|
|
bool codepoint_redef = false;
|
2023-03-20 14:52:42 +02:00
|
|
|
bool codepoint_sequence = false;
|
2023-02-22 22:28:12 +02:00
|
|
|
|
|
|
|
uint8_t bytes[4] {};
|
|
|
|
uint32_t byte_index = 0;
|
|
|
|
if (header.flags & PSF2_HAS_UNICODE_TABLE)
|
|
|
|
{
|
|
|
|
uint32_t glyph_index = 0;
|
|
|
|
for (uint32_t i = glyph_data_size + header.header_size; i < font_data.size(); i++)
|
|
|
|
{
|
|
|
|
uint8_t byte = font_data[i];
|
|
|
|
|
2023-03-20 19:46:37 +02:00
|
|
|
if (byte == PSF2_STARTSEQ)
|
2023-03-20 14:52:42 +02:00
|
|
|
{
|
|
|
|
codepoint_sequence = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-03-20 19:46:37 +02:00
|
|
|
else if (byte == PSF2_SEPARATOR)
|
2023-03-20 14:52:42 +02:00
|
|
|
{
|
|
|
|
if (byte_index)
|
|
|
|
{
|
|
|
|
invalid_utf = true;
|
|
|
|
byte_index = 0;
|
|
|
|
}
|
|
|
|
glyph_index++;
|
|
|
|
}
|
|
|
|
else
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
2023-03-20 14:52:42 +02:00
|
|
|
ASSERT(byte_index < 4);
|
|
|
|
bytes[byte_index++] = byte;
|
2023-03-20 19:46:37 +02:00
|
|
|
|
2023-03-23 11:13:14 +02:00
|
|
|
uint32_t len = BAN::UTF8::byte_length(bytes[0]);
|
2023-03-20 14:52:42 +02:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
invalid_utf = true;
|
|
|
|
byte_index = 0;
|
|
|
|
}
|
|
|
|
else if (len == byte_index)
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
2023-03-23 11:13:14 +02:00
|
|
|
uint32_t codepoint = BAN::UTF8::to_codepoint(bytes);
|
2023-03-20 14:52:42 +02:00
|
|
|
if (codepoint == BAN::UTF8::invalid)
|
|
|
|
invalid_utf = true;
|
2023-02-22 22:28:12 +02:00
|
|
|
else if (glyph_offsets.contains(codepoint))
|
|
|
|
codepoint_redef = true;
|
|
|
|
else
|
|
|
|
TRY(glyph_offsets.insert(codepoint, glyph_index * header.glyph_size));
|
2023-03-20 14:52:42 +02:00
|
|
|
byte_index = 0;
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < header.glyph_count; i++)
|
2024-01-24 14:43:46 +02:00
|
|
|
TRY(glyph_offsets.insert(i, i * header.glyph_size));
|
2023-02-22 22:28:12 +02:00
|
|
|
}
|
|
|
|
|
2023-03-20 14:52:42 +02:00
|
|
|
if (invalid_utf)
|
|
|
|
dwarnln("Font contains invalid UTF-8 codepoint(s)");
|
2023-02-22 22:28:12 +02:00
|
|
|
if (codepoint_redef)
|
2023-03-20 19:46:37 +02:00
|
|
|
dwarnln("Font contains multiple definitions for same codepoint(s)");
|
2023-03-20 14:52:42 +02:00
|
|
|
if (codepoint_sequence)
|
|
|
|
dwarnln("Font contains codepoint sequences (not supported)");
|
2023-02-22 22:28:12 +02:00
|
|
|
|
|
|
|
Font result;
|
|
|
|
result.m_glyph_offsets = BAN::move(glyph_offsets);
|
|
|
|
result.m_glyph_data = BAN::move(glyph_data);
|
|
|
|
result.m_width = header.width;
|
|
|
|
result.m_height = header.height;
|
|
|
|
result.m_pitch = header.glyph_size / header.height;
|
|
|
|
return result;
|
|
|
|
}
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-03-20 14:52:42 +02:00
|
|
|
bool Font::has_glyph(uint32_t codepoint) const
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
|
|
|
return m_glyph_offsets.contains(codepoint);
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:52:42 +02:00
|
|
|
const uint8_t* Font::glyph(uint32_t codepoint) const
|
2023-02-22 22:28:12 +02:00
|
|
|
{
|
|
|
|
return m_glyph_data.data() + m_glyph_offsets[codepoint];
|
|
|
|
}
|
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|