1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:37:34 +00:00

LibTTF: Memory map TTF fonts instead of reading them into heap memory

All GUI applications currently load all TTF fonts on startup
(to populate the Gfx::FontDatabase. This could probably be smarter.)

Before this patch, everyone would open the files and read them into
heap-allocated storage. Now we simply mmap() them instead. :^)
This commit is contained in:
Andreas Kling 2021-07-04 20:21:24 +02:00
parent 560109bd42
commit 49d0b9e808
3 changed files with 25 additions and 21 deletions

View file

@ -6,7 +6,6 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/HashMap.h>
#include <AK/Noncopyable.h>
#include <AK/RefCounted.h>
@ -47,7 +46,7 @@ class Font : public RefCounted<Font> {
public:
static Result<NonnullRefPtr<Font>, String> try_load_from_file(String path, unsigned index = 0);
static Result<NonnullRefPtr<Font>, String> try_load_from_memory(ByteBuffer&, unsigned index = 0);
static Result<NonnullRefPtr<Font>, String> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0);
ScaledFontMetrics metrics(float x_scale, float y_scale) const;
ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const;
@ -72,9 +71,10 @@ private:
TableRecord = 16,
};
static Result<NonnullRefPtr<Font>, String> try_load_from_offset(ByteBuffer&&, unsigned index = 0);
Font(ByteBuffer&& buffer, Head&& head, Name&& name, Hhea&& hhea, Maxp&& maxp, Hmtx&& hmtx, Cmap&& cmap, Loca&& loca, Glyf&& glyf)
: m_buffer(move(buffer))
static Result<NonnullRefPtr<Font>, String> try_load_from_offset(ReadonlyBytes, unsigned index = 0);
Font(ReadonlyBytes bytes, Head&& head, Name&& name, Hhea&& hhea, Maxp&& maxp, Hmtx&& hmtx, Cmap&& cmap, Loca&& loca, Glyf&& glyf)
: m_buffer(move(bytes))
, m_head(move(head))
, m_name(move(name))
, m_hhea(move(hhea))
@ -86,8 +86,10 @@ private:
{
}
// This owns the font data
ByteBuffer m_buffer;
RefPtr<MappedFile> m_mapped_file;
ReadonlyBytes m_buffer;
// These are stateful wrappers around non-owning slices
Head m_head;
Name m_name;