From ad243ef3aab049898363a5fa6b72a54224cd3e0a Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 1 Jan 2021 20:30:59 +0000 Subject: [PATCH] LibTTF: Add option to load font from a byte buffer --- Libraries/LibTTF/Font.cpp | 7 ++++++- Libraries/LibTTF/Font.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Libraries/LibTTF/Font.cpp b/Libraries/LibTTF/Font.cpp index 2b53332bf1..981478e27b 100644 --- a/Libraries/LibTTF/Font.cpp +++ b/Libraries/LibTTF/Font.cpp @@ -208,6 +208,11 @@ RefPtr Font::load_from_file(const StringView& path, unsigned index) return nullptr; } auto buffer = file->read_all(); + return load_from_memory(buffer, index); +} + +RefPtr Font::load_from_memory(ByteBuffer& buffer, unsigned index) +{ if (buffer.size() < 4) { dbg() << "Font file too small"; return nullptr; @@ -227,7 +232,7 @@ RefPtr Font::load_from_file(const StringView& path, unsigned index) return nullptr; } if (tag != 0x00010000) { - dbg() << "Not a valid font"; + dbg() << "Not a valid font"; return nullptr; } return load_from_offset(move(buffer), 0); diff --git a/Libraries/LibTTF/Font.h b/Libraries/LibTTF/Font.h index 3062cb1190..9250dd9a9d 100644 --- a/Libraries/LibTTF/Font.h +++ b/Libraries/LibTTF/Font.h @@ -65,6 +65,7 @@ class Font : public RefCounted { public: static RefPtr load_from_file(const StringView& path, unsigned index = 0); + static RefPtr load_from_memory(ByteBuffer&, 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;