Browse Source

LibGfx: Add helpers to load BitmapFont from Core::Resource

Andrew Kaster 1 year ago
parent
commit
d587bd0a04

+ 20 - 1
Userland/Libraries/LibGfx/Font/BitmapFont.cpp

@@ -11,6 +11,7 @@
 #include <AK/Utf32View.h>
 #include <AK/Utf8View.h>
 #include <LibCore/File.h>
+#include <LibCore/Resource.h>
 #include <LibCore/System.h>
 #include <LibGfx/Font/FontDatabase.h>
 #include <LibGfx/Font/FontStyleMapping.h>
@@ -224,13 +225,31 @@ ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::try_load_from_stream(FixedMemoryS
     return font;
 }
 
+ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::try_load_from_resource(NonnullRefPtr<Core::Resource> resource)
+{
+    auto stream = resource->stream();
+    auto font = TRY(try_load_from_stream(stream));
+    font->m_owned_data = move(resource);
+    return font;
+}
+
 ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::try_load_from_mapped_file(NonnullOwnPtr<Core::MappedFile> mapped_file)
 {
     auto font = TRY(try_load_from_stream(*mapped_file));
-    font->m_mapped_file = move(mapped_file);
+    font->m_owned_data = move(mapped_file);
     return font;
 }
 
+NonnullRefPtr<BitmapFont> BitmapFont::load_from_uri(StringView uri)
+{
+    return MUST(try_load_from_uri(uri));
+}
+
+ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::try_load_from_uri(StringView uri)
+{
+    return try_load_from_resource(TRY(Core::Resource::load_from_uri(uri)));
+}
+
 RefPtr<BitmapFont> BitmapFont::load_from_file(DeprecatedString const& path)
 {
     return MUST(try_load_from_file(move(path)));

+ 5 - 1
Userland/Libraries/LibGfx/Font/BitmapFont.h

@@ -13,6 +13,7 @@
 #include <AK/Types.h>
 #include <AK/Vector.h>
 #include <LibCore/MappedFile.h>
+#include <LibCore/Resource.h>
 #include <LibGfx/Font/Font.h>
 #include <LibGfx/Size.h>
 
@@ -29,7 +30,10 @@ public:
     ErrorOr<NonnullRefPtr<BitmapFont>> masked_character_set() const;
     ErrorOr<NonnullRefPtr<BitmapFont>> unmasked_character_set() const;
 
+    static NonnullRefPtr<BitmapFont> load_from_uri(StringView);
     static RefPtr<BitmapFont> load_from_file(DeprecatedString const& path);
+    static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_uri(StringView);
+    static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_resource(NonnullRefPtr<Core::Resource>);
     static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_file(DeprecatedString const& path);
     static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_mapped_file(NonnullOwnPtr<Core::MappedFile>);
     static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_stream(FixedMemoryStream&);
@@ -151,7 +155,7 @@ private:
 
     Bytes m_rows;
     Span<u8> m_glyph_widths;
-    OwnPtr<Core::MappedFile> m_mapped_file;
+    Variant<Empty, NonnullOwnPtr<Core::MappedFile>, NonnullRefPtr<Core::Resource>> m_owned_data = Empty {};
 
     u8 m_glyph_width { 0 };
     u8 m_glyph_height { 0 };