summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/Bitmap.cpp
diff options
context:
space:
mode:
authorKarol Kosek <krkk@serenityos.org>2022-08-29 19:53:27 +0200
committerLinus Groh <mail@linusgroh.de>2022-09-01 14:27:07 +0100
commit5478409025481ae0ba93b056842cbed48cc2971f (patch)
tree34dab503c88d2504e62929c9b48a5c1e5b5f966e /Userland/Libraries/LibGfx/Bitmap.cpp
parent406dff16d1d78741fbe351ab8f6df5509fc18521 (diff)
downloadserenity-5478409025481ae0ba93b056842cbed48cc2971f.zip
LibGfx: Fallback to a default scaled bitmap if the upscaled one failed
This makes cursors in a 2x Display Scale without a special upscaled version (such as a Hand cursor) display the 1x version instead.
Diffstat (limited to 'Userland/Libraries/LibGfx/Bitmap.cpp')
-rw-r--r--Userland/Libraries/LibGfx/Bitmap.cpp40
1 files changed, 26 insertions, 14 deletions
diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp
index e4b8178099..d3338dcb61 100644
--- a/Userland/Libraries/LibGfx/Bitmap.cpp
+++ b/Userland/Libraries/LibGfx/Bitmap.cpp
@@ -105,20 +105,32 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_create_wrapper(BitmapFormat format, I
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_load_from_file(StringView path, int scale_factor)
{
if (scale_factor > 1 && path.starts_with("/res/"sv)) {
- LexicalPath lexical_path { path };
- StringBuilder highdpi_icon_path;
- TRY(highdpi_icon_path.try_appendff("{}/{}-{}x.{}", lexical_path.dirname(), lexical_path.title(), scale_factor, lexical_path.extension()));
-
- auto highdpi_icon_string = highdpi_icon_path.string_view();
- auto fd = TRY(Core::System::open(highdpi_icon_string, O_RDONLY));
-
- auto bitmap = TRY(try_load_from_fd_and_close(fd, highdpi_icon_string));
- if (bitmap->width() % scale_factor != 0 || bitmap->height() % scale_factor != 0)
- return Error::from_string_literal("Bitmap::try_load_from_file: HighDPI image size should be divisible by scale factor");
- bitmap->m_size.set_width(bitmap->width() / scale_factor);
- bitmap->m_size.set_height(bitmap->height() / scale_factor);
- bitmap->m_scale = scale_factor;
- return bitmap;
+ auto load_scaled_bitmap = [](StringView path, int scale_factor) -> ErrorOr<NonnullRefPtr<Bitmap>> {
+ LexicalPath lexical_path { path };
+ StringBuilder highdpi_icon_path;
+ TRY(highdpi_icon_path.try_appendff("{}/{}-{}x.{}", lexical_path.dirname(), lexical_path.title(), scale_factor, lexical_path.extension()));
+
+ auto highdpi_icon_string = highdpi_icon_path.string_view();
+ auto fd = TRY(Core::System::open(highdpi_icon_string, O_RDONLY));
+
+ auto bitmap = TRY(try_load_from_fd_and_close(fd, highdpi_icon_string));
+ if (bitmap->width() % scale_factor != 0 || bitmap->height() % scale_factor != 0)
+ return Error::from_string_literal("Bitmap::try_load_from_file: HighDPI image size should be divisible by scale factor");
+ bitmap->m_size.set_width(bitmap->width() / scale_factor);
+ bitmap->m_size.set_height(bitmap->height() / scale_factor);
+ bitmap->m_scale = scale_factor;
+ return bitmap;
+ };
+
+ auto scaled_bitmap_or_error = load_scaled_bitmap(path, scale_factor);
+ if (!scaled_bitmap_or_error.is_error())
+ return scaled_bitmap_or_error.release_value();
+
+ auto error = scaled_bitmap_or_error.release_error();
+ if (!(error.is_syscall() && error.code() == ENOENT)) {
+ dbgln("Couldn't load scaled bitmap: {}", error);
+ dbgln("Trying base scale instead.");
+ }
}
auto fd = TRY(Core::System::open(path, O_RDONLY));