Skip to content

Commit 07bceb8

Browse files
AtkinsSJawesomekling
authored andcommitted
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.
1 parent 27b63fe commit 07bceb8

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

Userland/Libraries/LibGfx/Line.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <AK/Format.h>
910
#include <AK/Optional.h>
1011
#include <AK/StdLibExtras.h>
1112
#include <AK/String.h>
@@ -153,3 +154,15 @@ inline String FloatLine::to_string() const
153154
}
154155

155156
}
157+
158+
namespace AK {
159+
160+
template<typename T>
161+
struct Formatter<Gfx::Line<T>> : Formatter<FormatString> {
162+
ErrorOr<void> format(FormatBuilder& builder, Gfx::Line<T> const& value)
163+
{
164+
return Formatter<FormatString>::format(builder, "[{},{} -> {},{}]"sv, value.a().x(), value.a().y(), value.b().x(), value.b().y());
165+
}
166+
};
167+
168+
}

Userland/Libraries/LibGfx/Point.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,10 @@ inline Point<T> cubic_interpolate(Point<T> const& p1, Point<T> const& p2, Point<
285285
namespace AK {
286286

287287
template<typename T>
288-
struct Formatter<Gfx::Point<T>> : Formatter<StringView> {
288+
struct Formatter<Gfx::Point<T>> : Formatter<FormatString> {
289289
ErrorOr<void> format(FormatBuilder& builder, Gfx::Point<T> const& value)
290290
{
291-
return Formatter<StringView>::format(builder, value.to_string());
291+
return Formatter<FormatString>::format(builder, "[{},{}]"sv, value.x(), value.y());
292292
}
293293
};
294294

Userland/Libraries/LibGfx/Rect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,10 +1026,10 @@ using FloatRect = Rect<float>;
10261026
namespace AK {
10271027

10281028
template<typename T>
1029-
struct Formatter<Gfx::Rect<T>> : Formatter<StringView> {
1029+
struct Formatter<Gfx::Rect<T>> : Formatter<FormatString> {
10301030
ErrorOr<void> format(FormatBuilder& builder, Gfx::Rect<T> const& value)
10311031
{
1032-
return Formatter<StringView>::format(builder, value.to_string());
1032+
return Formatter<FormatString>::format(builder, "[{},{} {}x{}]"sv, value.x(), value.y(), value.width(), value.height());
10331033
}
10341034
};
10351035

Userland/Libraries/LibGfx/Size.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ using FloatSize = Size<float>;
183183
namespace AK {
184184

185185
template<typename T>
186-
struct Formatter<Gfx::Size<T>> : Formatter<StringView> {
186+
struct Formatter<Gfx::Size<T>> : Formatter<FormatString> {
187187
ErrorOr<void> format(FormatBuilder& builder, Gfx::Size<T> const& value)
188188
{
189-
return Formatter<StringView>::format(builder, value.to_string());
189+
return Formatter<FormatString>::format(builder, "[{}x{}]"sv, value.width(), value.height());
190190
}
191191
};
192192

0 commit comments

Comments
 (0)