diff options
author | Andreas Kling <kling@serenityos.org> | 2021-07-04 20:21:24 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-04 21:34:26 +0200 |
commit | 49d0b9e808584663da116b2dc50e7dfc2c247b60 (patch) | |
tree | cdb1cf0c8c654157ee641505fd6839341321c0ee /Userland/Libraries/LibTTF/Font.cpp | |
parent | 560109bd42006c15170521c69a0d50d141ef65ec (diff) | |
download | serenity-49d0b9e808584663da116b2dc50e7dfc2c247b60.zip |
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. :^)
Diffstat (limited to 'Userland/Libraries/LibTTF/Font.cpp')
-rw-r--r-- | Userland/Libraries/LibTTF/Font.cpp | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/Userland/Libraries/LibTTF/Font.cpp b/Userland/Libraries/LibTTF/Font.cpp index 0405b6f696..8cc9d2c6f0 100644 --- a/Userland/Libraries/LibTTF/Font.cpp +++ b/Userland/Libraries/LibTTF/Font.cpp @@ -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" }; |