Userland+Tests: Remove uses of direct file loading for BitmapFont

Route them through Core::Resource APIs instead.
This commit is contained in:
Andrew Kaster 2023-10-17 12:45:22 -06:00 committed by Andrew Kaster
parent d587bd0a04
commit 1567332e34
6 changed files with 28 additions and 26 deletions

View file

@ -15,15 +15,15 @@
#include <stdlib.h>
#include <unistd.h>
#ifdef AK_OS_SERENITY
# define TEST_INPUT(x) ("/usr/Tests/LibGfx/test-inputs/" x)
#else
# define TEST_INPUT(x) ("test-inputs/" x)
#endif
static void init_font_database()
{
Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(TEST_INPUT(""_string)));
#ifdef AK_OS_SERENITY
auto const test_file_root = "/usr/Tests/LibGfx/test-inputs/"_string;
#else
auto const test_file_root = "test-inputs/"_string;
#endif
Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(test_file_root));
Gfx::FontDatabase::the().load_all_fonts_from_uri("resource:///"sv);
}
@ -141,9 +141,10 @@ TEST_CASE(test_glyph_or_emoji_width)
EXPECT(font->glyph_or_emoji_width(it));
}
TEST_CASE(test_load_from_file)
TEST_CASE(test_load_from_uri)
{
auto font = Gfx::BitmapFont::load_from_file(TEST_INPUT("TestFont.font"sv));
init_font_database();
auto font = Gfx::BitmapFont::load_from_uri("resource://TestFont.font"sv);
EXPECT(!font->name().is_empty());
}
@ -161,7 +162,8 @@ TEST_CASE(test_write_to_file)
TEST_CASE(test_character_set_masking)
{
auto font = TRY_OR_FAIL(Gfx::BitmapFont::try_load_from_file(TEST_INPUT("TestFont.font"sv)));
init_font_database();
auto font = TRY_OR_FAIL(Gfx::BitmapFont::try_load_from_uri("resource://TestFont.font"sv));
auto unmasked_font = TRY_OR_FAIL(font->unmasked_character_set());
EXPECT(unmasked_font->glyph_index(0x0041).value() == 0x0041);

View file

@ -32,7 +32,7 @@ ErrorOr<void> WelcomeWidget::create_widgets()
TRY(load_from_gml(welcome_window_gml));
m_banner_widget = find_descendant_of_type_named<GUI::Widget>("welcome_banner");
m_banner_font = TRY(Gfx::BitmapFont::try_load_from_file("/res/fonts/MarietaRegular24.font"sv));
m_banner_font = TRY(Gfx::BitmapFont::try_load_from_uri("resource://fonts/MarietaRegular24.font"sv));
m_web_view = find_descendant_of_type_named<WebView::OutOfProcessWebView>("web_view");
m_web_view->use_native_user_style_sheet();

View file

@ -154,7 +154,7 @@ void Canvas::draw()
painter.draw_text(Gfx::IntRect { 520, 450, 240, 20 }, "Normal text (fixed width)"sv, Gfx::FontDatabase::default_fixed_width_font(), Gfx::TextAlignment::CenterLeft, Color::Blue);
painter.draw_text(Gfx::IntRect { 520, 465, 240, 20 }, "Bold text (fixed width)"sv, Gfx::FontDatabase::default_fixed_width_font().bold_variant(), Gfx::TextAlignment::CenterLeft, Color::Yellow);
auto font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonBold14.font"sv);
auto font = Gfx::BitmapFont::load_from_uri("resource://fonts/PebbletonBold14.font"sv);
painter.draw_rect({ 520, 510, 240, 30 }, Color::DarkGray);
painter.draw_text(Gfx::IntRect { 520, 510, 240, 30 }, "Hello friends! :^)"sv, *font, Gfx::TextAlignment::Center, Color::White);

View file

@ -56,7 +56,7 @@ ColorLines::ColorLines(StringView app_name)
, m_board { make<MarbleBoard>() }
, m_marble_bitmaps { build_marble_color_bitmaps() }
, m_trace_bitmaps { build_marble_trace_bitmaps() }
, m_score_font { Gfx::BitmapFont::load_from_file("/res/fonts/MarietaBold24.font") }
, m_score_font { Gfx::BitmapFont::load_from_uri("resource://fonts/MarietaBold24.font"sv) }
{
VERIFY(m_marble_bitmaps.size() == Marble::number_of_colors);
set_font(Gfx::FontDatabase::default_fixed_width_font().bold_variant());

View file

@ -20,10 +20,10 @@ REGISTER_WIDGET(GUI, Calendar);
namespace GUI {
static auto const extra_large_font = Gfx::BitmapFont::load_from_file("/res/fonts/MarietaRegular36.font");
static auto const large_font = Gfx::BitmapFont::load_from_file("/res/fonts/MarietaRegular24.font");
static auto const medium_font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonRegular14.font");
static auto const small_font = Gfx::BitmapFont::load_from_file("/res/fonts/KaticaRegular10.font");
static auto const extra_large_font = Gfx::BitmapFont::load_from_uri("resource://fonts/MarietaRegular36.font"sv);
static auto const large_font = Gfx::BitmapFont::load_from_uri("resource://fonts/MarietaRegular24.font"sv);
static auto const medium_font = Gfx::BitmapFont::load_from_uri("resource://fonts/PebbletonRegular14.font"sv);
static auto const small_font = Gfx::BitmapFont::load_from_uri("resource://fonts/KaticaRegular10.font"sv);
Calendar::Calendar(Core::DateTime date_time, Mode mode)
: m_selected_date(date_time)
@ -451,7 +451,7 @@ void Calendar::paint_event(GUI::PaintEvent& event)
m_months[i].is_being_pressed,
m_months[i].is_hovered,
false, true, false);
set_font(small_font);
set_font(*small_font);
painter.draw_text(month_tile_rect, m_months[i].name, font(), Gfx::TextAlignment::Center, palette().base_text());
i++;
}
@ -623,15 +623,15 @@ void Calendar::paint_tile(GUI::Painter& painter, GUI::Calendar::Tile& tile, Gfx:
font().pixel_size_rounded_up() + 4);
if (width > 150 && height > 150) {
set_font(extra_large_font);
set_font(*extra_large_font);
} else if (width > 100 && height > 100) {
set_font(large_font);
set_font(*large_font);
} else if (width > 50 && height > 50) {
set_font(medium_font);
set_font(*medium_font);
} else if (width >= 30 && height >= 30) {
set_font(small_font);
set_font(*small_font);
} else {
set_font(small_font);
set_font(*small_font);
text_alignment = Gfx::TextAlignment::Center;
text_rect = Gfx::IntRect(tile_rect);
}
@ -654,9 +654,9 @@ void Calendar::paint_tile(GUI::Painter& painter, GUI::Calendar::Tile& tile, Gfx:
painter.fill_rect(tile_rect, palette().base());
if (width > 50 && height > 50) {
set_font(medium_font);
set_font(*medium_font);
} else {
set_font(small_font);
set_font(*small_font);
}
auto display_date = DeprecatedString::number(tile.day);

View file

@ -122,7 +122,7 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri)
auto path_string = resource.filesystem_path().release_value();
auto path = LexicalPath(path_string.bytes_as_string_view());
if (path.has_extension(".font"sv)) {
if (auto font_or_error = Gfx::BitmapFont::try_load_from_file(path.string()); !font_or_error.is_error()) {
if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
auto font = font_or_error.release_value();
m_private->full_name_to_font_map.set(font->qualified_name().to_deprecated_string(), *font);
auto typeface = get_or_create_typeface(font->family(), font->variant());