2018-10-12 00:45:57 +03:00
|
|
|
#include "Font.h"
|
2019-02-03 01:13:12 +03:00
|
|
|
#include <AK/BufferStream.h>
|
2019-04-03 14:51:49 +03:00
|
|
|
#include <AK/MappedFile.h>
|
2019-06-07 12:46:55 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/kmalloc.h>
|
2019-02-03 01:13:12 +03:00
|
|
|
#include <LibC/errno.h>
|
2019-06-07 12:46:55 +03:00
|
|
|
#include <LibC/fcntl.h>
|
2019-02-03 03:36:25 +03:00
|
|
|
#include <LibC/mman.h>
|
2019-06-07 12:46:55 +03:00
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/unistd.h>
|
2019-09-04 23:45:55 +03:00
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
#include "Emoji.h"
|
|
|
|
#include "GraphicsBitmap.h"
|
2019-01-25 17:12:23 +03:00
|
|
|
|
2019-06-07 12:46:55 +03:00
|
|
|
struct [[gnu::packed]] FontFileHeader
|
|
|
|
{
|
2019-02-05 08:43:33 +03:00
|
|
|
char magic[4];
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 glyph_width;
|
|
|
|
u8 glyph_height;
|
|
|
|
u8 type;
|
|
|
|
u8 is_variable_width;
|
|
|
|
u8 unused[6];
|
2019-02-05 08:43:33 +03:00
|
|
|
char name[64];
|
2019-02-15 14:30:48 +03:00
|
|
|
};
|
2019-02-05 08:43:33 +03:00
|
|
|
|
2019-01-16 19:54:06 +03:00
|
|
|
Font& Font::default_font()
|
2018-10-12 00:45:57 +03:00
|
|
|
{
|
2019-03-06 16:06:40 +03:00
|
|
|
static Font* s_default_font;
|
|
|
|
static const char* default_font_path = "/res/fonts/Katica10.font";
|
2019-02-03 01:13:12 +03:00
|
|
|
if (!s_default_font) {
|
2019-02-03 03:42:34 +03:00
|
|
|
s_default_font = Font::load_from_file(default_font_path).leak_ref();
|
2019-02-04 13:37:15 +03:00
|
|
|
ASSERT(s_default_font);
|
2019-02-03 01:13:12 +03:00
|
|
|
}
|
2019-01-11 01:19:29 +03:00
|
|
|
return *s_default_font;
|
2019-02-04 13:37:15 +03:00
|
|
|
}
|
|
|
|
|
2019-03-06 16:06:40 +03: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-08-04 09:14:53 +03:00
|
|
|
Font& Font::default_bold_fixed_width_font()
|
|
|
|
{
|
|
|
|
static Font* font;
|
|
|
|
static const char* default_bold_fixed_width_font_path = "/res/fonts/CsillaBold7x10.font";
|
|
|
|
if (!font) {
|
|
|
|
font = Font::load_from_file(default_bold_fixed_width_font_path).leak_ref();
|
|
|
|
ASSERT(font);
|
|
|
|
}
|
|
|
|
return *font;
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:37:15 +03:00
|
|
|
Font& Font::default_bold_font()
|
|
|
|
{
|
2019-03-06 16:06:40 +03:00
|
|
|
static Font* s_default_bold_font;
|
2019-03-06 16:50:27 +03:00
|
|
|
static const char* default_bold_font_path = "/res/fonts/KaticaBold10.font";
|
2019-02-04 13:37:15 +03: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-12 00:45:57 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Font> Font::clone() const
|
2019-02-02 10:05:14 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
size_t bytes_per_glyph = sizeof(u32) * glyph_height();
|
2019-02-02 10:05:14 +03:00
|
|
|
// FIXME: This is leaked!
|
2019-02-05 08:43:33 +03:00
|
|
|
auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * 256));
|
|
|
|
memcpy(new_rows, m_rows, bytes_per_glyph * 256);
|
2019-07-03 22:17:35 +03:00
|
|
|
auto* new_widths = static_cast<u8*>(kmalloc(256));
|
2019-03-06 14:52:41 +03:00
|
|
|
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 10:05:14 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
Font::Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height)
|
2019-02-03 01:13:12 +03:00
|
|
|
: m_name(name)
|
2019-02-05 08:43:33 +03:00
|
|
|
, m_rows(rows)
|
2019-03-06 14:52:41 +03:00
|
|
|
, m_glyph_widths(widths)
|
2019-01-16 19:54:06 +03:00
|
|
|
, m_glyph_width(glyph_width)
|
|
|
|
, m_glyph_height(glyph_height)
|
2019-03-06 13:03:10 +03:00
|
|
|
, m_min_glyph_width(glyph_width)
|
|
|
|
, m_max_glyph_width(glyph_width)
|
2019-03-06 14:52:41 +03:00
|
|
|
, m_fixed_width(is_fixed_width)
|
2018-10-12 00:45:57 +03:00
|
|
|
{
|
2019-03-06 13:03:10 +03:00
|
|
|
if (!m_fixed_width) {
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 maximum = 0;
|
|
|
|
u8 minimum = 255;
|
2019-03-06 14:52:41 +03:00
|
|
|
for (int i = 0; i < 256; ++i) {
|
2019-03-06 13:03:10 +03:00
|
|
|
minimum = min(minimum, m_glyph_widths[i]);
|
2019-03-06 14:52:41 +03:00
|
|
|
maximum = max(maximum, m_glyph_widths[i]);
|
|
|
|
}
|
2019-03-06 13:03:10 +03:00
|
|
|
m_min_glyph_width = minimum;
|
2019-03-06 14:52:41 +03:00
|
|
|
m_max_glyph_width = maximum;
|
2019-03-06 13:03:10 +03:00
|
|
|
}
|
2018-10-12 00:45:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::~Font()
|
|
|
|
{
|
|
|
|
}
|
2019-02-03 01:13:12 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
RefPtr<Font> Font::load_from_memory(const u8* data)
|
2019-02-03 01:13:12 +03:00
|
|
|
{
|
2019-02-03 03:36:25 +03:00
|
|
|
auto& header = *reinterpret_cast<const FontFileHeader*>(data);
|
2019-02-03 01:13:12 +03:00
|
|
|
if (memcmp(header.magic, "!Fnt", 4)) {
|
2019-02-03 03:36:25 +03: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-03 01:13:12 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (header.name[63] != '\0') {
|
|
|
|
dbgprintf("Font name not fully null-terminated\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:52:41 +03:00
|
|
|
size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
|
|
|
|
|
2019-06-22 15:41:11 +03:00
|
|
|
auto* rows = const_cast<unsigned*>((const unsigned*)(data + sizeof(FontFileHeader)));
|
2019-07-03 22:17:35 +03:00
|
|
|
u8* widths = nullptr;
|
2019-03-06 14:52:41 +03:00
|
|
|
if (header.is_variable_width)
|
2019-07-03 22:17:35 +03:00
|
|
|
widths = (u8*)(rows) + 256 * bytes_per_glyph;
|
2019-03-06 14:52:41 +03:00
|
|
|
return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height));
|
2019-02-03 01:13:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Font> Font::load_from_file(const StringView& path)
|
2019-02-03 03:36:25 +03:00
|
|
|
{
|
2019-04-03 14:51:49 +03:00
|
|
|
MappedFile mapped_file(path);
|
|
|
|
if (!mapped_file.is_valid())
|
2019-02-03 03:36:25 +03:00
|
|
|
return nullptr;
|
2019-02-03 10:17:35 +03:00
|
|
|
|
2019-09-30 09:57:01 +03:00
|
|
|
auto font = load_from_memory((const u8*)mapped_file.data());
|
2019-04-03 14:51:49 +03:00
|
|
|
font->m_mapped_file = move(mapped_file);
|
2019-02-03 03:36:25 +03:00
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
bool Font::write_to_file(const StringView& path)
|
2019-02-03 01:13:12 +03:00
|
|
|
{
|
2019-07-08 21:01:49 +03:00
|
|
|
int fd = creat_with_path_length(path.characters_without_null_termination(), path.length(), 0644);
|
2019-02-03 01:13:12 +03:00
|
|
|
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 14:52:41 +03:00
|
|
|
header.is_variable_width = !m_fixed_width;
|
2019-02-26 00:06:55 +03:00
|
|
|
memcpy(header.name, m_name.characters(), min(m_name.length(), 63));
|
2019-02-03 01:13:12 +03:00
|
|
|
|
|
|
|
size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
|
|
|
|
|
2019-03-06 14:52:41 +03:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (256 * bytes_per_glyph) + 256);
|
2019-02-03 01:13:12 +03:00
|
|
|
BufferStream stream(buffer);
|
|
|
|
|
2019-03-17 02:36:41 +03: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-03 01:13:12 +03:00
|
|
|
|
|
|
|
ASSERT(stream.at_end());
|
2019-09-30 09:57:01 +03:00
|
|
|
ssize_t nwritten = write(fd, buffer.data(), buffer.size());
|
2019-02-03 01:13:12 +03:00
|
|
|
ASSERT(nwritten == (ssize_t)buffer.size());
|
|
|
|
int rc = close(fd);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-06 13:03:10 +03:00
|
|
|
|
2019-09-04 23:45:55 +03:00
|
|
|
int Font::glyph_or_emoji_width(u32 codepoint) const
|
2019-03-06 13:03:10 +03:00
|
|
|
{
|
2019-09-04 23:45:55 +03:00
|
|
|
if (codepoint < 256)
|
|
|
|
return glyph_width((char)codepoint);
|
2019-03-25 15:35:24 +03:00
|
|
|
|
2019-03-06 13:03:10 +03:00
|
|
|
if (m_fixed_width)
|
2019-09-04 23:45:55 +03:00
|
|
|
return m_glyph_width;
|
|
|
|
|
2019-10-19 19:36:45 +03:00
|
|
|
auto* emoji = Emoji::emoji_for_codepoint(codepoint);
|
2019-09-04 23:45:55 +03:00
|
|
|
if (emoji == nullptr)
|
|
|
|
return glyph_width('?');
|
2019-10-19 19:36:45 +03:00
|
|
|
return emoji->size().width();
|
2019-09-04 23:45:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int Font::width(const StringView& string) const
|
|
|
|
{
|
|
|
|
Utf8View utf8 { string };
|
|
|
|
return width(utf8);
|
|
|
|
}
|
2019-03-06 13:03:10 +03:00
|
|
|
|
2019-09-04 23:45:55 +03:00
|
|
|
int Font::width(const Utf8View& utf8) const
|
|
|
|
{
|
|
|
|
bool first = true;
|
2019-03-06 13:03:10 +03:00
|
|
|
int width = 0;
|
2019-03-25 15:35:24 +03:00
|
|
|
|
2019-09-04 23:45:55 +03:00
|
|
|
for (u32 codepoint : utf8) {
|
|
|
|
if (!first)
|
|
|
|
width += glyph_spacing();
|
|
|
|
first = false;
|
|
|
|
width += glyph_or_emoji_width(codepoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
2019-03-06 13:03:10 +03:00
|
|
|
}
|