2018-10-11 23:45:57 +02:00
|
|
|
#include "Font.h"
|
2019-02-02 17:42:16 +01:00
|
|
|
#include <AK/kmalloc.h>
|
2019-02-02 23:13:12 +01:00
|
|
|
#include <AK/BufferStream.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
#include <LibC/unistd.h>
|
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/fcntl.h>
|
|
|
|
#include <LibC/errno.h>
|
2019-02-03 01:36:25 +01:00
|
|
|
#include <LibC/mman.h>
|
2019-01-25 15:12:23 +01:00
|
|
|
|
2019-02-15 12:30:48 +01:00
|
|
|
struct [[gnu::packed]] FontFileHeader {
|
2019-02-05 06:43:33 +01:00
|
|
|
char magic[4];
|
|
|
|
byte glyph_width;
|
|
|
|
byte glyph_height;
|
|
|
|
byte type;
|
2019-03-06 12:52:41 +01:00
|
|
|
byte is_variable_width;
|
|
|
|
byte unused[6];
|
2019-02-05 06:43:33 +01:00
|
|
|
char name[64];
|
2019-02-15 12:30:48 +01:00
|
|
|
};
|
2019-02-05 06:43:33 +01:00
|
|
|
|
2019-01-16 17:54:06 +01:00
|
|
|
Font& Font::default_font()
|
2018-10-11 23:45:57 +02:00
|
|
|
{
|
2019-03-06 14:06:40 +01:00
|
|
|
static Font* s_default_font;
|
|
|
|
static const char* default_font_path = "/res/fonts/Katica10.font";
|
2019-02-02 23:13:12 +01:00
|
|
|
if (!s_default_font) {
|
2019-02-03 01:42:34 +01:00
|
|
|
s_default_font = Font::load_from_file(default_font_path).leak_ref();
|
2019-02-04 11:37:15 +01:00
|
|
|
ASSERT(s_default_font);
|
2019-02-02 23:13:12 +01:00
|
|
|
}
|
2019-01-10 23:19:29 +01:00
|
|
|
return *s_default_font;
|
2019-02-04 11:37:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 14:06:40 +01:00
|
|
|
Font& Font::default_fixed_width_font()
|
|
|
|
{
|
|
|
|
static Font* s_default_fixed_width_font;
|
|
|
|
static const char* default_fixed_width_font_path = "/res/fonts/CsillaThin7x10.font";
|
|
|
|
if (!s_default_fixed_width_font) {
|
|
|
|
s_default_fixed_width_font = Font::load_from_file(default_fixed_width_font_path).leak_ref();
|
|
|
|
ASSERT(s_default_fixed_width_font);
|
|
|
|
}
|
|
|
|
return *s_default_fixed_width_font;
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:37:15 +01:00
|
|
|
Font& Font::default_bold_font()
|
|
|
|
{
|
2019-03-06 14:06:40 +01:00
|
|
|
static Font* s_default_bold_font;
|
2019-03-06 14:50:27 +01:00
|
|
|
static const char* default_bold_font_path = "/res/fonts/KaticaBold10.font";
|
2019-02-04 11:37:15 +01:00
|
|
|
if (!s_default_bold_font) {
|
|
|
|
s_default_bold_font = Font::load_from_file(default_bold_font_path).leak_ref();
|
|
|
|
ASSERT(s_default_bold_font);
|
|
|
|
}
|
|
|
|
return *s_default_bold_font;
|
2018-10-11 23:45:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-02 08:05:14 +01:00
|
|
|
RetainPtr<Font> Font::clone() const
|
|
|
|
{
|
2019-02-05 06:43:33 +01:00
|
|
|
size_t bytes_per_glyph = sizeof(dword) * glyph_height();
|
2019-02-02 08:05:14 +01:00
|
|
|
// FIXME: This is leaked!
|
2019-02-05 06:43:33 +01:00
|
|
|
auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * 256));
|
|
|
|
memcpy(new_rows, m_rows, bytes_per_glyph * 256);
|
2019-03-06 12:52:41 +01:00
|
|
|
auto* new_widths = static_cast<byte*>(kmalloc(256));
|
|
|
|
if (m_glyph_widths)
|
|
|
|
memcpy(new_widths, m_glyph_widths, 256);
|
|
|
|
else
|
|
|
|
memset(new_widths, m_glyph_width, 256);
|
|
|
|
return adopt(*new Font(m_name, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height));
|
2019-02-02 08:05:14 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 12:52:41 +01:00
|
|
|
Font::Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width, byte glyph_width, byte glyph_height)
|
2019-02-02 23:13:12 +01:00
|
|
|
: m_name(name)
|
2019-02-05 06:43:33 +01:00
|
|
|
, m_rows(rows)
|
2019-03-06 12:52:41 +01:00
|
|
|
, m_glyph_widths(widths)
|
2019-01-16 17:54:06 +01:00
|
|
|
, m_glyph_width(glyph_width)
|
|
|
|
, m_glyph_height(glyph_height)
|
2019-03-06 11:03:10 +01:00
|
|
|
, m_min_glyph_width(glyph_width)
|
|
|
|
, m_max_glyph_width(glyph_width)
|
2019-03-06 12:52:41 +01:00
|
|
|
, m_fixed_width(is_fixed_width)
|
2018-10-11 23:45:57 +02:00
|
|
|
{
|
2019-03-06 11:03:10 +01:00
|
|
|
if (!m_fixed_width) {
|
2019-03-06 12:52:41 +01:00
|
|
|
byte maximum = 0;
|
2019-03-06 11:03:10 +01:00
|
|
|
byte minimum = 255;
|
2019-03-06 12:52:41 +01:00
|
|
|
for (int i = 0; i < 256; ++i) {
|
2019-03-06 11:03:10 +01:00
|
|
|
minimum = min(minimum, m_glyph_widths[i]);
|
2019-03-06 12:52:41 +01:00
|
|
|
maximum = max(maximum, m_glyph_widths[i]);
|
|
|
|
}
|
2019-03-06 11:03:10 +01:00
|
|
|
m_min_glyph_width = minimum;
|
2019-03-06 12:52:41 +01:00
|
|
|
m_max_glyph_width = maximum;
|
2019-03-06 11:03:10 +01:00
|
|
|
}
|
2018-10-11 23:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::~Font()
|
|
|
|
{
|
2019-02-17 00:13:47 +01:00
|
|
|
if (m_mmap_ptr) {
|
|
|
|
int rc = munmap(m_mmap_ptr, 4096 * 3);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
2018-10-11 23:45:57 +02:00
|
|
|
}
|
2019-02-02 23:13:12 +01:00
|
|
|
|
2019-02-03 01:36:25 +01:00
|
|
|
RetainPtr<Font> Font::load_from_memory(const byte* data)
|
2019-02-02 23:13:12 +01:00
|
|
|
{
|
2019-02-03 01:36:25 +01:00
|
|
|
auto& header = *reinterpret_cast<const FontFileHeader*>(data);
|
2019-02-02 23:13:12 +01:00
|
|
|
if (memcmp(header.magic, "!Fnt", 4)) {
|
2019-02-03 01:36:25 +01:00
|
|
|
dbgprintf("header.magic != '!Fnt', instead it's '%c%c%c%c'\n", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
|
2019-02-02 23:13:12 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (header.name[63] != '\0') {
|
|
|
|
dbgprintf("Font name not fully null-terminated\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-06 12:52:41 +01:00
|
|
|
size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
|
|
|
|
|
2019-02-05 06:43:33 +01:00
|
|
|
auto* rows = (unsigned*)(data + sizeof(FontFileHeader));
|
2019-03-06 12:52:41 +01:00
|
|
|
byte* widths = nullptr;
|
|
|
|
if (header.is_variable_width)
|
|
|
|
widths = (byte*)(rows) + 256 * bytes_per_glyph;
|
|
|
|
return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height));
|
2019-02-02 23:13:12 +01:00
|
|
|
}
|
|
|
|
|
2019-02-03 01:36:25 +01:00
|
|
|
RetainPtr<Font> Font::load_from_file(const String& path)
|
|
|
|
{
|
|
|
|
int fd = open(path.characters(), O_RDONLY, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
|
|
|
|
perror("open");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* mapped_file = (byte*)mmap(nullptr, 4096 * 3, PROT_READ, MAP_SHARED, fd, 0);
|
2019-02-03 08:17:35 +01:00
|
|
|
if (mapped_file == MAP_FAILED) {
|
|
|
|
int rc = close(fd);
|
|
|
|
ASSERT(rc == 0);
|
2019-02-03 01:36:25 +01:00
|
|
|
return nullptr;
|
2019-02-03 08:17:35 +01:00
|
|
|
}
|
2019-02-03 01:36:25 +01:00
|
|
|
|
|
|
|
auto font = load_from_memory(mapped_file);
|
2019-02-17 00:13:47 +01:00
|
|
|
font->m_mmap_ptr = mapped_file;
|
2019-02-03 08:17:35 +01:00
|
|
|
|
2019-02-05 06:43:33 +01:00
|
|
|
int rc = close(fd);
|
2019-02-03 08:17:35 +01:00
|
|
|
ASSERT(rc == 0);
|
2019-02-03 01:36:25 +01:00
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2019-02-02 23:13:12 +01:00
|
|
|
bool Font::write_to_file(const String& path)
|
|
|
|
{
|
|
|
|
int fd = open(path.characters(), O_WRONLY | O_CREAT, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontFileHeader header;
|
|
|
|
memset(&header, 0, sizeof(FontFileHeader));
|
|
|
|
memcpy(header.magic, "!Fnt", 4);
|
|
|
|
header.glyph_width = m_glyph_width;
|
|
|
|
header.glyph_height = m_glyph_height;
|
|
|
|
header.type = 0;
|
2019-03-06 12:52:41 +01:00
|
|
|
header.is_variable_width = !m_fixed_width;
|
2019-02-25 22:06:55 +01:00
|
|
|
memcpy(header.name, m_name.characters(), min(m_name.length(), 63));
|
2019-02-02 23:13:12 +01:00
|
|
|
|
|
|
|
size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
|
|
|
|
|
2019-03-06 12:52:41 +01:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (256 * bytes_per_glyph) + 256);
|
2019-02-02 23:13:12 +01:00
|
|
|
BufferStream stream(buffer);
|
|
|
|
|
2019-03-17 00:36:41 +01:00
|
|
|
stream << ByteBuffer::wrap(&header, sizeof(FontFileHeader));
|
|
|
|
stream << ByteBuffer::wrap(m_rows, (256 * bytes_per_glyph));
|
|
|
|
stream << ByteBuffer::wrap(m_glyph_widths, 256);
|
2019-02-02 23:13:12 +01:00
|
|
|
|
|
|
|
ASSERT(stream.at_end());
|
|
|
|
ssize_t nwritten = write(fd, buffer.pointer(), buffer.size());
|
|
|
|
ASSERT(nwritten == (ssize_t)buffer.size());
|
|
|
|
int rc = close(fd);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-06 11:03:10 +01:00
|
|
|
|
|
|
|
int Font::width(const String& string) const
|
|
|
|
{
|
2019-03-25 13:35:24 +01:00
|
|
|
if (string.is_empty())
|
|
|
|
return 0;
|
|
|
|
|
2019-03-06 11:03:10 +01:00
|
|
|
if (m_fixed_width)
|
|
|
|
return string.length() * m_glyph_width;
|
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
for (int i = 0; i < string.length(); ++i)
|
2019-03-06 12:52:41 +01:00
|
|
|
width += glyph_width(string[i]) + 1;
|
2019-03-25 13:35:24 +01:00
|
|
|
|
|
|
|
return width - 1;
|
2019-03-06 11:03:10 +01:00
|
|
|
}
|