From 1967d5bc75916d69e7df628125c2e57bdf4cc4ea Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 1 May 2025 16:08:22 +0100 Subject: [PATCH] LibGfx: Add a helper for constructing a TTF font from temporary bytes Once we switch from ResourceLoader to Fetch, nobody is going to be holding onto the downloaded data, so we need to make a permanent copy of it. --- Libraries/LibGfx/Font/Typeface.cpp | 7 +++++++ Libraries/LibGfx/Font/Typeface.h | 1 + 2 files changed, 8 insertions(+) diff --git a/Libraries/LibGfx/Font/Typeface.cpp b/Libraries/LibGfx/Font/Typeface.cpp index 08ffeaa0e10..be97e371550 100644 --- a/Libraries/LibGfx/Font/Typeface.cpp +++ b/Libraries/LibGfx/Font/Typeface.cpp @@ -25,6 +25,13 @@ ErrorOr> Typeface::try_load_from_font_data(NonnullOwnPtr return typeface; } +ErrorOr> Typeface::try_load_from_temporary_memory(ReadonlyBytes bytes, int ttc_index) +{ + auto buffer = TRY(ByteBuffer::copy(bytes)); + auto font_data = FontData::create_from_byte_buffer(move(buffer)); + return try_load_from_font_data(move(font_data), ttc_index); +} + ErrorOr> Typeface::try_load_from_externally_owned_memory(ReadonlyBytes bytes, int ttc_index) { return TypefaceSkia::load_from_buffer(bytes, ttc_index); diff --git a/Libraries/LibGfx/Font/Typeface.h b/Libraries/LibGfx/Font/Typeface.h index 38741693e4a..5daf9d6493d 100644 --- a/Libraries/LibGfx/Font/Typeface.h +++ b/Libraries/LibGfx/Font/Typeface.h @@ -38,6 +38,7 @@ class Typeface : public RefCounted { public: static ErrorOr> try_load_from_resource(Core::Resource const&, int ttc_index = 0); static ErrorOr> try_load_from_font_data(NonnullOwnPtr, int ttc_index = 0); + static ErrorOr> try_load_from_temporary_memory(ReadonlyBytes bytes, int ttc_index = 0); static ErrorOr> try_load_from_externally_owned_memory(ReadonlyBytes bytes, int ttc_index = 0); virtual ~Typeface();