diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-10-26 20:26:14 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-10-27 13:05:16 +0200 |
commit | 07bceb861a5453a44d2868a505ef9c9f27c9dbfa (patch) | |
tree | 5af5bfff2a7b0382984df186abf2618456c20028 /Userland/Libraries/LibGfx | |
parent | 27b63feae5a214064372a6b24bd25f63923b4e85 (diff) | |
download | serenity-07bceb861a5453a44d2868a505ef9c9f27c9dbfa.zip |
LibGfx: Make formatting of spatial types work with non-int/floats
Line, Point, Rect, and Size now all have Formatters that will work with
any type that itself has a Formatter.
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r-- | Userland/Libraries/LibGfx/Line.h | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Point.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Rect.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Size.h | 4 |
4 files changed, 19 insertions, 6 deletions
diff --git a/Userland/Libraries/LibGfx/Line.h b/Userland/Libraries/LibGfx/Line.h index f1438cd1b5..4cfff5124e 100644 --- a/Userland/Libraries/LibGfx/Line.h +++ b/Userland/Libraries/LibGfx/Line.h @@ -6,6 +6,7 @@ #pragma once +#include <AK/Format.h> #include <AK/Optional.h> #include <AK/StdLibExtras.h> #include <AK/String.h> @@ -153,3 +154,15 @@ inline String FloatLine::to_string() const } } + +namespace AK { + +template<typename T> +struct Formatter<Gfx::Line<T>> : Formatter<FormatString> { + ErrorOr<void> format(FormatBuilder& builder, Gfx::Line<T> const& value) + { + return Formatter<FormatString>::format(builder, "[{},{} -> {},{}]"sv, value.a().x(), value.a().y(), value.b().x(), value.b().y()); + } +}; + +} diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 51a775f722..360eb91db3 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -285,10 +285,10 @@ inline Point<T> cubic_interpolate(Point<T> const& p1, Point<T> const& p2, Point< namespace AK { template<typename T> -struct Formatter<Gfx::Point<T>> : Formatter<StringView> { +struct Formatter<Gfx::Point<T>> : Formatter<FormatString> { ErrorOr<void> format(FormatBuilder& builder, Gfx::Point<T> const& value) { - return Formatter<StringView>::format(builder, value.to_string()); + return Formatter<FormatString>::format(builder, "[{},{}]"sv, value.x(), value.y()); } }; diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index 819d1b1dd7..4a204c5a4a 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -1026,10 +1026,10 @@ using FloatRect = Rect<float>; namespace AK { template<typename T> -struct Formatter<Gfx::Rect<T>> : Formatter<StringView> { +struct Formatter<Gfx::Rect<T>> : Formatter<FormatString> { ErrorOr<void> format(FormatBuilder& builder, Gfx::Rect<T> const& value) { - return Formatter<StringView>::format(builder, value.to_string()); + return Formatter<FormatString>::format(builder, "[{},{} {}x{}]"sv, value.x(), value.y(), value.width(), value.height()); } }; diff --git a/Userland/Libraries/LibGfx/Size.h b/Userland/Libraries/LibGfx/Size.h index 79aa3c1a57..dc8722048b 100644 --- a/Userland/Libraries/LibGfx/Size.h +++ b/Userland/Libraries/LibGfx/Size.h @@ -183,10 +183,10 @@ using FloatSize = Size<float>; namespace AK { template<typename T> -struct Formatter<Gfx::Size<T>> : Formatter<StringView> { +struct Formatter<Gfx::Size<T>> : Formatter<FormatString> { ErrorOr<void> format(FormatBuilder& builder, Gfx::Size<T> const& value) { - return Formatter<StringView>::format(builder, value.to_string()); + return Formatter<FormatString>::format(builder, "[{}x{}]"sv, value.width(), value.height()); } }; |