mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:07:36 +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:
parent
560109bd42
commit
49d0b9e808
3 changed files with 25 additions and 21 deletions
|
@ -8,9 +8,8 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.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_externally_owned_memory({ data, size });
|
||||||
(void)TTF::Font::try_load_from_memory(font_data);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
|
* Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
|
||||||
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "AK/ByteBuffer.h"
|
|
||||||
#include <AK/Checked.h>
|
#include <AK/Checked.h>
|
||||||
|
#include <AK/MappedFile.h>
|
||||||
#include <AK/Utf32View.h>
|
#include <AK/Utf32View.h>
|
||||||
#include <AK/Utf8View.h>
|
#include <AK/Utf8View.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
@ -15,6 +16,7 @@
|
||||||
#include <LibTTF/Tables.h>
|
#include <LibTTF/Tables.h>
|
||||||
#include <LibTextCodec/Decoder.h>
|
#include <LibTextCodec/Decoder.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
namespace TTF {
|
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)
|
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())
|
if (file_or_error.is_error())
|
||||||
return file_or_error.error();
|
return String { file_or_error.error().string() };
|
||||||
|
|
||||||
auto file = file_or_error.value();
|
auto& file = *file_or_error.value();
|
||||||
if (!file->open(Core::OpenMode::ReadOnly))
|
auto result = try_load_from_externally_owned_memory(file.bytes(), index);
|
||||||
return String { "Could not open file" };
|
if (result.is_error())
|
||||||
|
return result.error();
|
||||||
auto buffer = file->read_all();
|
auto& font = *result.value();
|
||||||
return try_load_from_memory(buffer, index);
|
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)
|
if (buffer.size() < 4)
|
||||||
return String { "Font file too small" };
|
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.
|
// 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))
|
if (Checked<u32>::addition_would_overflow(offset, (u32)Sizes::OffsetTable))
|
||||||
return String { "Invalid offset in font header" };
|
return String { "Invalid offset in font header" };
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/ByteBuffer.h>
|
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/Noncopyable.h>
|
#include <AK/Noncopyable.h>
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
|
@ -47,7 +46,7 @@ class Font : public RefCounted<Font> {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Result<NonnullRefPtr<Font>, String> try_load_from_file(String path, unsigned index = 0);
|
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;
|
ScaledFontMetrics metrics(float x_scale, float y_scale) const;
|
||||||
ScaledGlyphMetrics glyph_metrics(u32 glyph_id, 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,
|
TableRecord = 16,
|
||||||
};
|
};
|
||||||
|
|
||||||
static Result<NonnullRefPtr<Font>, String> try_load_from_offset(ByteBuffer&&, unsigned index = 0);
|
static Result<NonnullRefPtr<Font>, String> try_load_from_offset(ReadonlyBytes, 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))
|
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_head(move(head))
|
||||||
, m_name(move(name))
|
, m_name(move(name))
|
||||||
, m_hhea(move(hhea))
|
, m_hhea(move(hhea))
|
||||||
|
@ -86,8 +86,10 @@ private:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// This owns the font data
|
RefPtr<MappedFile> m_mapped_file;
|
||||||
ByteBuffer m_buffer;
|
|
||||||
|
ReadonlyBytes m_buffer;
|
||||||
|
|
||||||
// These are stateful wrappers around non-owning slices
|
// These are stateful wrappers around non-owning slices
|
||||||
Head m_head;
|
Head m_head;
|
||||||
Name m_name;
|
Name m_name;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue