1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 23:58:11 +00:00

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.
This commit is contained in:
Sam Atkins 2022-10-26 20:26:14 +01:00 committed by Andreas Kling
parent 27b63feae5
commit 07bceb861a
4 changed files with 19 additions and 6 deletions

View file

@ -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());
}
};
}

View file

@ -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());
}
};

View file

@ -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());
}
};

View file

@ -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());
}
};