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
Notes: sideshowbarker 2024-07-18 10:27:30 +09:00
3 changed files with 25 additions and 21 deletions

View file

@ -8,9 +8,8 @@
#include <stddef.h>
#include <stdint.h>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
extern "C" int LLVMFuzzerTestOneInput(u8 const* data, size_t size)
{
ByteBuffer font_data = ByteBuffer::copy(data, size);
(void)TTF::Font::try_load_from_memory(font_data);
(void)TTF::Font::try_load_from_externally_owned_memory({ data, size });
return 0;
}

View file

@ -1,11 +1,12 @@
/*
* Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "AK/ByteBuffer.h"
#include <AK/Checked.h>
#include <AK/MappedFile.h>
#include <AK/Utf32View.h>
#include <AK/Utf8View.h>
#include <LibCore/File.h>
@ -15,6 +16,7 @@
#include <LibTTF/Tables.h>
#include <LibTextCodec/Decoder.h>
#include <math.h>
#include <sys/mman.h>
namespace TTF {
@ -207,19 +209,20 @@ GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
Result<NonnullRefPtr<Font>, String> Font::try_load_from_file(String path, unsigned index)
{
auto file_or_error = Core::File::open(move(path), Core::OpenMode::ReadOnly);
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return file_or_error.error();
return String { file_or_error.error().string() };
auto file = file_or_error.value();
if (!file->open(Core::OpenMode::ReadOnly))
return String { "Could not open file" };
auto buffer = file->read_all();
return try_load_from_memory(buffer, index);
auto& file = *file_or_error.value();
auto result = try_load_from_externally_owned_memory(file.bytes(), index);
if (result.is_error())
return result.error();
auto& font = *result.value();
font.m_mapped_file = file_or_error.release_value();
return result;
}
Result<NonnullRefPtr<Font>, String> Font::try_load_from_memory(ByteBuffer& buffer, unsigned index)
Result<NonnullRefPtr<Font>, String> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, unsigned index)
{
if (buffer.size() < 4)
return String { "Font file too small" };
@ -243,7 +246,7 @@ Result<NonnullRefPtr<Font>, String> Font::try_load_from_memory(ByteBuffer& buffe
}
// FIXME: "loca" and "glyf" are not available for CFF fonts.
Result<NonnullRefPtr<Font>, String> Font::try_load_from_offset(ByteBuffer&& buffer, u32 offset)
Result<NonnullRefPtr<Font>, String> Font::try_load_from_offset(ReadonlyBytes buffer, u32 offset)
{
if (Checked<u32>::addition_would_overflow(offset, (u32)Sizes::OffsetTable))
return String { "Invalid offset in font header" };

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;