mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 15:27:35 +00:00
mmap all the font files!
Font now uses the same in-memory format as the font files we have on disk. This allows us to simply mmap() the font files and not use any additional memory for them. Very cool! :^) Hacking on this exposed a bug in file-backed VMObjects where the first client to instantiate a VMObject for a specific inode also got to decide its size. Since file-backed VMObjects always have the same size as the underlying file, this made no sense, so I removed the ability to even set a size in that case.
This commit is contained in:
parent
612c02307e
commit
a258d6507a
11 changed files with 134 additions and 110 deletions
|
@ -4,6 +4,8 @@
|
|||
#include <AK/StdLibExtras.h>
|
||||
|
||||
#ifdef KERNEL
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/MemoryManager.h>
|
||||
#include <Kernel/FileDescriptor.h>
|
||||
#include <Kernel/VirtualFileSystem.h>
|
||||
#endif
|
||||
|
@ -34,6 +36,20 @@ static constexpr const char* error_glyph {
|
|||
static Font* s_default_font;
|
||||
static Font* s_default_bold_font;
|
||||
|
||||
struct FontFileHeader {
|
||||
char magic[4];
|
||||
byte glyph_width;
|
||||
byte glyph_height;
|
||||
byte type;
|
||||
byte unused[7];
|
||||
char name[64];
|
||||
} PACKED;
|
||||
|
||||
static inline constexpr size_t font_file_size(unsigned glyph_height)
|
||||
{
|
||||
return sizeof(FontFileHeader) + 256 * sizeof(dword) * glyph_height;
|
||||
}
|
||||
|
||||
void Font::initialize()
|
||||
{
|
||||
s_default_font = nullptr;
|
||||
|
@ -53,9 +69,10 @@ Font& Font::default_font()
|
|||
kprintf("Failed to open default font (%s)\n", default_font_path);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
auto buffer = descriptor->read_entire_file(*current);
|
||||
ASSERT(buffer);
|
||||
s_default_font = Font::load_from_memory(buffer.pointer()).leak_ref();
|
||||
auto* region = current->allocate_file_backed_region(LinearAddress(), font_file_size(10), descriptor->inode(), "default_font", /*readable*/true, /*writable*/false);
|
||||
ASSERT(region);
|
||||
region->page_in();
|
||||
s_default_font = Font::load_from_memory(region->laddr().as_ptr()).leak_ref();
|
||||
#endif
|
||||
ASSERT(s_default_font);
|
||||
}
|
||||
|
@ -72,12 +89,15 @@ Font& Font::default_bold_font()
|
|||
int error;
|
||||
auto descriptor = VFS::the().open(default_bold_font_path, error, 0, 0, *VFS::the().root_inode());
|
||||
if (!descriptor) {
|
||||
kprintf("Failed to open default font (%s)\n", default_bold_font_path);
|
||||
kprintf("Failed to open default bold font (%s)\n", default_bold_font_path);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
auto buffer = descriptor->read_entire_file(*current);
|
||||
ASSERT(buffer);
|
||||
s_default_bold_font = Font::load_from_memory(buffer.pointer()).leak_ref();
|
||||
auto* region = current->allocate_file_backed_region(LinearAddress(), font_file_size(10), descriptor->inode(), "default_bold_font", /*readable*/true, /*writable*/false);
|
||||
ASSERT(region);
|
||||
ASSERT_INTERRUPTS_ENABLED();
|
||||
region->page_in();
|
||||
ASSERT_INTERRUPTS_ENABLED();
|
||||
s_default_bold_font = Font::load_from_memory(region->laddr().as_ptr()).leak_ref();
|
||||
#endif
|
||||
ASSERT(s_default_bold_font);
|
||||
}
|
||||
|
@ -86,54 +106,28 @@ Font& Font::default_bold_font()
|
|||
|
||||
RetainPtr<Font> Font::clone() const
|
||||
{
|
||||
size_t bytes_per_glyph = glyph_width() * glyph_height();
|
||||
size_t bytes_per_glyph = sizeof(dword) * glyph_height();
|
||||
// FIXME: This is leaked!
|
||||
char** new_glyphs = static_cast<char**>(kmalloc(sizeof(char*) * 256));
|
||||
for (unsigned i = 0; i < 256; ++i) {
|
||||
new_glyphs[i] = static_cast<char*>(kmalloc(bytes_per_glyph));
|
||||
if (i >= m_first_glyph && i <= m_last_glyph) {
|
||||
memcpy(new_glyphs[i], m_glyphs[i - m_first_glyph], bytes_per_glyph);
|
||||
} else {
|
||||
memset(new_glyphs[i], ' ', bytes_per_glyph);
|
||||
}
|
||||
}
|
||||
return adopt(*new Font(m_name, new_glyphs, m_glyph_width, m_glyph_height, 0, 255));
|
||||
auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * 256));
|
||||
memcpy(new_rows, m_rows, bytes_per_glyph * 256);
|
||||
return adopt(*new Font(m_name, new_rows, m_glyph_width, m_glyph_height));
|
||||
}
|
||||
|
||||
Font::Font(const String& name, const char* const* glyphs, byte glyph_width, byte glyph_height, byte first_glyph, byte last_glyph)
|
||||
Font::Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height)
|
||||
: m_name(name)
|
||||
, m_glyphs(glyphs)
|
||||
, m_rows(rows)
|
||||
, m_glyph_width(glyph_width)
|
||||
, m_glyph_height(glyph_height)
|
||||
, m_first_glyph(first_glyph)
|
||||
, m_last_glyph(last_glyph)
|
||||
{
|
||||
ASSERT(m_glyph_width == error_glyph_width);
|
||||
ASSERT(m_glyph_height == error_glyph_height);
|
||||
m_error_bitmap = CharacterBitmap::create_from_ascii(error_glyph, error_glyph_width, error_glyph_height);
|
||||
for (unsigned ch = 0; ch < 256; ++ch) {
|
||||
if (ch < m_first_glyph || ch > m_last_glyph) {
|
||||
m_bitmaps[ch] = m_error_bitmap.copy_ref();
|
||||
continue;
|
||||
}
|
||||
const char* data = m_glyphs[(unsigned)ch - m_first_glyph];
|
||||
m_bitmaps[ch] = CharacterBitmap::create_from_ascii(data, m_glyph_width, m_glyph_height);
|
||||
}
|
||||
}
|
||||
|
||||
Font::~Font()
|
||||
{
|
||||
}
|
||||
|
||||
struct FontFileHeader {
|
||||
char magic[4];
|
||||
byte glyph_width;
|
||||
byte glyph_height;
|
||||
byte type;
|
||||
byte unused[7];
|
||||
char name[64];
|
||||
} PACKED;
|
||||
|
||||
RetainPtr<Font> Font::load_from_memory(const byte* data)
|
||||
{
|
||||
auto& header = *reinterpret_cast<const FontFileHeader*>(data);
|
||||
|
@ -146,25 +140,8 @@ RetainPtr<Font> Font::load_from_memory(const byte* data)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
auto* glyphs_ptr = reinterpret_cast<const unsigned*>(data + sizeof(FontFileHeader));
|
||||
|
||||
char** new_glyphs = static_cast<char**>(kmalloc(sizeof(char*) * 256));
|
||||
for (unsigned glyph_index = 0; glyph_index < 256; ++glyph_index) {
|
||||
new_glyphs[glyph_index] = static_cast<char*>(kmalloc(header.glyph_width * header.glyph_height));
|
||||
char* bitptr = new_glyphs[glyph_index];
|
||||
for (unsigned y = 0; y < header.glyph_height; ++y) {
|
||||
unsigned pattern = *(glyphs_ptr++);
|
||||
for (unsigned x = 0; x < header.glyph_width; ++x) {
|
||||
if (pattern & (1u << x)) {
|
||||
*(bitptr++) = '#';
|
||||
} else {
|
||||
*(bitptr++) = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return adopt(*new Font(String(header.name), new_glyphs, header.glyph_width, header.glyph_height, 0, 255));
|
||||
auto* rows = (unsigned*)(data + sizeof(FontFileHeader));
|
||||
return adopt(*new Font(String(header.name), rows, header.glyph_width, header.glyph_height));
|
||||
}
|
||||
|
||||
#ifdef USERLAND
|
||||
|
@ -185,10 +162,8 @@ RetainPtr<Font> Font::load_from_file(const String& path)
|
|||
}
|
||||
|
||||
auto font = load_from_memory(mapped_file);
|
||||
int rc = munmap(mapped_file, 4096 * 3);
|
||||
ASSERT(rc == 0);
|
||||
|
||||
rc = close(fd);
|
||||
int rc = close(fd);
|
||||
ASSERT(rc == 0);
|
||||
return font;
|
||||
}
|
||||
|
@ -215,19 +190,7 @@ bool Font::write_to_file(const String& path)
|
|||
BufferStream stream(buffer);
|
||||
|
||||
stream << ByteBuffer::wrap((byte*)&header, sizeof(FontFileHeader));
|
||||
|
||||
for (unsigned glyph_index = 0; glyph_index < 256; ++glyph_index) {
|
||||
auto* glyph_bits = (byte*)m_glyphs[glyph_index];
|
||||
for (unsigned y = 0; y < m_glyph_height; ++y) {
|
||||
unsigned pattern = 0;
|
||||
for (unsigned x = 0; x < m_glyph_width; ++x) {
|
||||
if (glyph_bits[y * m_glyph_width + x] == '#') {
|
||||
pattern |= 1 << x;
|
||||
}
|
||||
}
|
||||
stream << pattern;
|
||||
}
|
||||
}
|
||||
stream << ByteBuffer::wrap((byte*)m_rows, (256 * bytes_per_glyph));
|
||||
|
||||
ASSERT(stream.at_end());
|
||||
ssize_t nwritten = write(fd, buffer.pointer(), buffer.size());
|
||||
|
|
|
@ -6,6 +6,38 @@
|
|||
#include <AK/AKString.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
// FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
|
||||
class GlyphBitmap {
|
||||
friend class Font;
|
||||
public:
|
||||
const unsigned* rows() const { return m_rows; }
|
||||
unsigned row(unsigned index) const { return m_rows[index]; }
|
||||
|
||||
bool bit_at(int x, int y) const { return row(y) & (1 << x); }
|
||||
void set_bit_at(int x, int y, bool b)
|
||||
{
|
||||
auto& mutable_row = const_cast<unsigned*>(m_rows)[y];
|
||||
if (b)
|
||||
mutable_row |= 1 << x;
|
||||
else
|
||||
mutable_row &= ~(1 << x);
|
||||
}
|
||||
|
||||
Size size() const { return m_size; }
|
||||
int width() const { return m_size.width(); }
|
||||
int height() const { return m_size.height(); }
|
||||
|
||||
private:
|
||||
GlyphBitmap(const unsigned* rows, Size size)
|
||||
: m_rows(rows)
|
||||
, m_size(size)
|
||||
{
|
||||
}
|
||||
|
||||
const unsigned* m_rows { nullptr };
|
||||
Size m_size;
|
||||
};
|
||||
|
||||
class Font : public Retainable<Font> {
|
||||
public:
|
||||
static Font& default_font();
|
||||
|
@ -22,7 +54,7 @@ public:
|
|||
|
||||
~Font();
|
||||
|
||||
const CharacterBitmap& glyph_bitmap(char ch) const { return *m_bitmaps[(byte)ch]; }
|
||||
GlyphBitmap glyph_bitmap(char ch) const { return GlyphBitmap(&m_rows[(byte)ch * m_glyph_height], { m_glyph_width, m_glyph_height }); }
|
||||
|
||||
byte glyph_width() const { return m_glyph_width; }
|
||||
byte glyph_height() const { return m_glyph_height; }
|
||||
|
@ -33,17 +65,14 @@ public:
|
|||
static void initialize();
|
||||
|
||||
private:
|
||||
Font(const String& name, const char* const* glyphs, byte glyph_width, byte glyph_height, byte first_glyph, byte last_glyph);
|
||||
Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height);
|
||||
|
||||
String m_name;
|
||||
|
||||
const char* const* m_glyphs { nullptr };
|
||||
mutable RetainPtr<CharacterBitmap> m_bitmaps[256];
|
||||
unsigned* m_rows { nullptr };
|
||||
|
||||
RetainPtr<CharacterBitmap> m_error_bitmap;
|
||||
|
||||
byte m_glyph_width { 0 };
|
||||
byte m_glyph_height { 0 };
|
||||
|
||||
byte m_first_glyph { 0 };
|
||||
byte m_last_glyph { 0 };
|
||||
};
|
||||
|
|
|
@ -206,6 +206,29 @@ void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color c
|
|||
}
|
||||
}
|
||||
|
||||
void Painter::draw_bitmap(const Point& p, const GlyphBitmap& bitmap, Color color)
|
||||
{
|
||||
Rect rect { p, bitmap.size() };
|
||||
rect.move_by(m_translation);
|
||||
auto clipped_rect = Rect::intersection(rect, m_clip_rect);
|
||||
if (clipped_rect.is_empty())
|
||||
return;
|
||||
const int first_row = clipped_rect.top() - rect.top();
|
||||
const int last_row = clipped_rect.bottom() - rect.top();
|
||||
const int first_column = clipped_rect.left() - rect.left();
|
||||
const int last_column = clipped_rect.right() - rect.left();
|
||||
RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
|
||||
const size_t dst_skip = m_target->width();
|
||||
|
||||
for (int row = first_row; row <= last_row; ++row) {
|
||||
for (int j = 0; j <= (last_column - first_column); ++j) {
|
||||
if (bitmap.bit_at(j, row))
|
||||
dst[j] = color.value();
|
||||
}
|
||||
dst += dst_skip;
|
||||
}
|
||||
}
|
||||
|
||||
FLATTEN void Painter::draw_glyph(const Point& point, char ch, Color color)
|
||||
{
|
||||
draw_bitmap(point, font().glyph_bitmap(ch), color);
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <AK/AKString.h>
|
||||
|
||||
class CharacterBitmap;
|
||||
class GlyphBitmap;
|
||||
class GraphicsBitmap;
|
||||
class Font;
|
||||
|
||||
|
@ -26,6 +27,7 @@ public:
|
|||
void fill_rect_with_gradient(const Rect&, Color gradient_start, Color gradient_end);
|
||||
void draw_rect(const Rect&, Color);
|
||||
void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color());
|
||||
void draw_bitmap(const Point&, const GlyphBitmap&, Color = Color());
|
||||
void set_pixel(const Point&, Color);
|
||||
void draw_line(const Point&, const Point&, Color);
|
||||
void draw_focus_rect(const Rect&);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue