1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

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

This commit is contained in:
Andrew Kaster 2023-10-17 12:07:39 -06:00 committed by Andrew Kaster
parent 897f4d05eb
commit d587bd0a04
2 changed files with 25 additions and 2 deletions

View file

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

View file

@ -13,6 +13,7 @@
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/MappedFile.h> #include <LibCore/MappedFile.h>
#include <LibCore/Resource.h>
#include <LibGfx/Font/Font.h> #include <LibGfx/Font/Font.h>
#include <LibGfx/Size.h> #include <LibGfx/Size.h>
@ -29,7 +30,10 @@ public:
ErrorOr<NonnullRefPtr<BitmapFont>> masked_character_set() const; ErrorOr<NonnullRefPtr<BitmapFont>> masked_character_set() const;
ErrorOr<NonnullRefPtr<BitmapFont>> unmasked_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 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_file(DeprecatedString const& path);
static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_mapped_file(NonnullOwnPtr<Core::MappedFile>); static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_mapped_file(NonnullOwnPtr<Core::MappedFile>);
static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_stream(FixedMemoryStream&); static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_stream(FixedMemoryStream&);
@ -151,7 +155,7 @@ private:
Bytes m_rows; Bytes m_rows;
Span<u8> m_glyph_widths; 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_width { 0 };
u8 m_glyph_height { 0 }; u8 m_glyph_height { 0 };