From 86ce502ae2ad103763c03dde45753072bad0e676 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 18 Oct 2023 14:12:42 -0600 Subject: [PATCH] LibGfx+Utilities: Add helpers to load vector fonts from Core::Resources --- Userland/Libraries/LibGfx/Font/FontDatabase.cpp | 8 +++----- Userland/Libraries/LibGfx/Font/OpenType/Font.cpp | 8 ++++---- Userland/Libraries/LibGfx/Font/OpenType/Font.h | 4 ++-- Userland/Libraries/LibGfx/Font/WOFF/Font.cpp | 6 +++--- Userland/Libraries/LibGfx/Font/WOFF/Font.h | 2 +- Userland/Libraries/LibGfx/Font/WOFF2/Font.cpp | 7 +++---- Userland/Libraries/LibGfx/Font/WOFF2/Font.h | 2 +- Userland/Utilities/ttfdisasm.cpp | 3 ++- 8 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/FontDatabase.cpp b/Userland/Libraries/LibGfx/Font/FontDatabase.cpp index e6cea5d905..ea3d880eca 100644 --- a/Userland/Libraries/LibGfx/Font/FontDatabase.cpp +++ b/Userland/Libraries/LibGfx/Font/FontDatabase.cpp @@ -118,9 +118,7 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri) auto root = root_or_error.release_value(); root->for_each_descendant_file([this](Core::Resource const& resource) -> IterationDecision { - // FIXME: Use Resources and their bytes/streams throughout so we don't have to use the path here - auto path_string = resource.filesystem_path().release_value(); - auto path = LexicalPath(path_string.bytes_as_string_view()); + auto path = LexicalPath(resource.uri().bytes_as_string_view()); if (path.has_extension(".font"sv)) { if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) { auto font = font_or_error.release_value(); @@ -130,13 +128,13 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri) } } else if (path.has_extension(".ttf"sv)) { // FIXME: What about .otf - if (auto font_or_error = OpenType::Font::try_load_from_file(path.string()); !font_or_error.is_error()) { + if (auto font_or_error = OpenType::Font::try_load_from_resource(resource); !font_or_error.is_error()) { auto font = font_or_error.release_value(); auto typeface = get_or_create_typeface(font->family(), font->variant()); typeface->set_vector_font(move(font)); } } else if (path.has_extension(".woff"sv)) { - if (auto font_or_error = WOFF::Font::try_load_from_file(path.string()); !font_or_error.is_error()) { + if (auto font_or_error = WOFF::Font::try_load_from_resource(resource); !font_or_error.is_error()) { auto font = font_or_error.release_value(); auto typeface = get_or_create_typeface(font->family(), font->variant()); typeface->set_vector_font(move(font)); diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp index 056199658e..50b4662615 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -350,11 +351,10 @@ GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const }; } -ErrorOr> Font::try_load_from_file(DeprecatedString path, unsigned index) +ErrorOr> Font::try_load_from_resource(Core::Resource const& resource, unsigned index) { - auto file = TRY(Core::MappedFile::map(path)); - auto font = TRY(try_load_from_externally_owned_memory(file->bytes(), index)); - font->m_mapped_file = move(file); + auto font = TRY(try_load_from_externally_owned_memory(resource.data(), index)); + font->m_resource = resource; return font; } diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.h b/Userland/Libraries/LibGfx/Font/OpenType/Font.h index b2b7f4cd67..c0e1f7e56b 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.h +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.h @@ -24,7 +24,7 @@ class Font : public Gfx::VectorFont { AK_MAKE_NONCOPYABLE(Font); public: - static ErrorOr> try_load_from_file(DeprecatedString path, unsigned index = 0); + static ErrorOr> try_load_from_resource(Core::Resource const&, unsigned index = 0); static ErrorOr> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0); virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override; @@ -107,7 +107,7 @@ private: { } - OwnPtr m_mapped_file; + RefPtr m_resource; ReadonlyBytes m_buffer; diff --git a/Userland/Libraries/LibGfx/Font/WOFF/Font.cpp b/Userland/Libraries/LibGfx/Font/WOFF/Font.cpp index a7ac8b5efd..86b7409096 100644 --- a/Userland/Libraries/LibGfx/Font/WOFF/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/WOFF/Font.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -68,10 +69,9 @@ static u16 pow_2_less_than_or_equal(u16 x) return 1 << (sizeof(u16) * 8 - count_leading_zeroes_safe(x - 1)); } -ErrorOr> Font::try_load_from_file(DeprecatedString path, unsigned int index) +ErrorOr> Font::try_load_from_resource(Core::Resource const& resource, unsigned index) { - auto file = TRY(Core::MappedFile::map(path)); - return try_load_from_externally_owned_memory(file->bytes(), index); + return try_load_from_externally_owned_memory(resource.data(), index); } ErrorOr> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, unsigned int index) diff --git a/Userland/Libraries/LibGfx/Font/WOFF/Font.h b/Userland/Libraries/LibGfx/Font/WOFF/Font.h index 8c68981b89..d2be0553ac 100644 --- a/Userland/Libraries/LibGfx/Font/WOFF/Font.h +++ b/Userland/Libraries/LibGfx/Font/WOFF/Font.h @@ -21,7 +21,7 @@ class Font : public Gfx::VectorFont { AK_MAKE_NONCOPYABLE(Font); public: - static ErrorOr> try_load_from_file(DeprecatedString path, unsigned index = 0); + static ErrorOr> try_load_from_resource(Core::Resource const&, unsigned index = 0); static ErrorOr> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0); virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override { return m_input_font->metrics(x_scale, y_scale); } diff --git a/Userland/Libraries/LibGfx/Font/WOFF2/Font.cpp b/Userland/Libraries/LibGfx/Font/WOFF2/Font.cpp index 41d9d93742..392f89afe9 100644 --- a/Userland/Libraries/LibGfx/Font/WOFF2/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/WOFF2/Font.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -831,10 +831,9 @@ static ErrorOr create_glyf_and_loca_tables_from_transfo return GlyfAndLocaTableBuffers { .glyf_table = move(reconstructed_glyf_table), .loca_table = move(loca_table_buffer) }; } -ErrorOr> Font::try_load_from_file(StringView path) +ErrorOr> Font::try_load_from_resource(Core::Resource const& resource) { - auto woff2_file_stream = TRY(Core::File::open(path, Core::File::OpenMode::Read)); - return try_load_from_externally_owned_memory(*woff2_file_stream); + return try_load_from_externally_owned_memory(resource.data()); } ErrorOr> Font::try_load_from_externally_owned_memory(ReadonlyBytes bytes) diff --git a/Userland/Libraries/LibGfx/Font/WOFF2/Font.h b/Userland/Libraries/LibGfx/Font/WOFF2/Font.h index 2f609766ab..1cfd6b1e67 100644 --- a/Userland/Libraries/LibGfx/Font/WOFF2/Font.h +++ b/Userland/Libraries/LibGfx/Font/WOFF2/Font.h @@ -21,7 +21,7 @@ class Font : public Gfx::VectorFont { AK_MAKE_NONCOPYABLE(Font); public: - static ErrorOr> try_load_from_file(StringView path); + static ErrorOr> try_load_from_resource(Core::Resource const&); static ErrorOr> try_load_from_externally_owned_memory(SeekableStream&); static ErrorOr> try_load_from_externally_owned_memory(ReadonlyBytes); diff --git a/Userland/Utilities/ttfdisasm.cpp b/Userland/Utilities/ttfdisasm.cpp index d21f9b561a..b6eb8e226e 100644 --- a/Userland/Utilities/ttfdisasm.cpp +++ b/Userland/Utilities/ttfdisasm.cpp @@ -168,7 +168,8 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(no_color, "Disable syntax highlighting", "no-color", 'n'); args_parser.parse(arguments); - auto font = TRY(OpenType::Font::try_load_from_file(font_path)); + auto resource = TRY(Core::Resource::load_from_filesystem(font_path)); + auto font = TRY(OpenType::Font::try_load_from_resource(resource)); if (dump_font_program) print_disassembly("Font program"sv, font->font_program(), !no_color);