1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 06:57:45 +00:00

LibGfx: Unify Rect, Point, and Size

This commit unifies methods and method/param names between the above
classes, as well as adds [[nodiscard]] and ALWAYS_INLINE where
appropriate. It also renamed the various move_by methods to
translate_by, as that more closely matches the transformation
terminology.
This commit is contained in:
Matthew Olsson 2021-04-12 11:47:09 -07:00 committed by Andreas Kling
parent ac238b3bd6
commit 88cfaf7bf0
48 changed files with 282 additions and 187 deletions

View file

@ -319,7 +319,7 @@ void ClassicStylePainter::paint_progressbar(Painter& painter, const IntRect& rec
float progress_height = progress * rect.height();
hole_rect = { 0, 0, rect.width(), (int)(rect.height() - progress_height) };
}
hole_rect.move_by(rect.location());
hole_rect.translate_by(rect.location());
hole_rect.set_right_without_resize(rect.right());
PainterStateSaver saver(painter);
painter.fill_rect(hole_rect, palette.base());

View file

@ -36,7 +36,7 @@ Gfx::IntRect ClassicWindowTheme::titlebar_icon_rect(WindowType window_type, cons
16,
};
icon_rect.center_vertically_within(titlebar_rect);
icon_rect.move_by(0, 1);
icon_rect.translate_by(0, 1);
return icon_rect;
}

View file

@ -56,7 +56,7 @@ void DisjointRectSet::shatter()
void DisjointRectSet::move_by(int dx, int dy)
{
for (auto& r : m_rects)
r.move_by(dx, dy);
r.translate_by(dx, dy);
}
bool DisjointRectSet::contains(const IntRect& rect) const

View file

@ -95,7 +95,7 @@ public:
// TODO: We probably don't need the entire source_rect, we could inflate
// the target_rect appropriately
apply_cache.m_target = Gfx::Bitmap::create(source.format(), source_rect.size());
target_rect.move_by(-target_rect.location());
target_rect.translate_by(-target_rect.location());
}
Bitmap* render_target_bitmap = (&target != &source) ? &target : apply_cache.m_target.ptr();

View file

@ -1070,28 +1070,28 @@ void draw_text_line(const IntRect& a_rect, const TextType& text, const Font& fon
if (is_vertically_centered_text_alignment(alignment)) {
int distance_from_baseline_to_bottom = (font.glyph_height() - 1) - font.baseline();
rect.move_by(0, distance_from_baseline_to_bottom / 2);
rect.translate_by(0, distance_from_baseline_to_bottom / 2);
}
auto point = rect.location();
int space_width = font.glyph_width(' ') + font.glyph_spacing();
if (direction == TextDirection::RTL) {
point.move_by(rect.width(), 0); // Start drawing from the end
space_width = -space_width; // Draw spaces backwards
point.translate_by(rect.width(), 0); // Start drawing from the end
space_width = -space_width; // Draw spaces backwards
}
for (u32 code_point : final_text) {
if (code_point == ' ') {
point.move_by(space_width, 0);
point.translate_by(space_width, 0);
continue;
}
IntSize glyph_size(font.glyph_or_emoji_width(code_point) + font.glyph_spacing(), font.glyph_height());
if (direction == TextDirection::RTL)
point.move_by(-glyph_size.width(), 0); // If we are drawing right to left, we have to move backwards before drawing the glyph
point.translate_by(-glyph_size.width(), 0); // If we are drawing right to left, we have to move backwards before drawing the glyph
draw_glyph({ point, glyph_size }, code_point);
if (direction == TextDirection::LTR)
point.move_by(glyph_size.width(), 0);
point.translate_by(glyph_size.width(), 0);
}
}
@ -1414,7 +1414,7 @@ void Painter::set_pixel(const IntPoint& p, Color color)
VERIFY(scale() == 1); // FIXME: Add scaling support.
auto point = p;
point.move_by(state().translation);
point.translate_by(state().translation);
if (!clip_rect().contains(point))
return;
m_target->scanline(point.y())[point.x()] = color.value();

View file

@ -100,7 +100,7 @@ public:
void clear_clip_rect();
void translate(int dx, int dy) { translate({ dx, dy }); }
void translate(const IntPoint& delta) { state().translation.move_by(delta); }
void translate(const IntPoint& delta) { state().translation.translate_by(delta); }
Gfx::Bitmap* target() { return m_target.ptr(); }

View file

@ -50,7 +50,7 @@ void Path::elliptical_arc_to(const FloatPoint& point, const FloatPoint& radii, d
}
// Move the endpoint by a small amount to avoid division by zero.
next_point.move_by(0.01f, 0.01f);
next_point.translate_by(0.01f, 0.01f);
}
// Find (cx, cy), theta_1, theta_delta

View file

@ -8,6 +8,7 @@
#include <AK/Format.h>
#include <AK/StdLibExtras.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Orientation.h>
#include <LibIPC/Forward.h>
@ -19,7 +20,7 @@ namespace Gfx {
template<typename T>
class Point {
public:
Point() { }
Point() = default;
Point(T x, T y)
: m_x(x)
@ -41,41 +42,74 @@ public:
{
}
T x() const { return m_x; }
T y() const { return m_y; }
[[nodiscard]] ALWAYS_INLINE T x() const { return m_x; }
[[nodiscard]] ALWAYS_INLINE T y() const { return m_y; }
void set_x(T x) { m_x = x; }
void set_y(T y) { m_y = y; }
ALWAYS_INLINE void set_x(T x) { m_x = x; }
ALWAYS_INLINE void set_y(T y) { m_y = y; }
void move_by(T dx, T dy)
[[nodiscard]] ALWAYS_INLINE bool is_null() const { return !m_x && !m_y; }
[[nodiscard]] ALWAYS_INLINE bool is_empty() const { return m_x <= 0 && m_y <= 0; }
void translate_by(T dx, T dy)
{
m_x += dx;
m_y += dy;
}
void move_by(const Point<T>& delta)
ALWAYS_INLINE void translate_by(T dboth) { translate_by(dboth, dboth); }
ALWAYS_INLINE void translate_by(const Point<T>& delta) { translate_by(delta.x(), delta.y()); }
void scale_by(T dx, T dy)
{
move_by(delta.x(), delta.y());
m_x *= dx;
m_y *= dy;
}
ALWAYS_INLINE void scale_by(T dboth) { scale_by(dboth, dboth); }
ALWAYS_INLINE void scale_by(const Point<T>& delta) { scale_by(delta.x(), delta.y()); }
void transform_by(const AffineTransform& transform) { *this = transform.map(*this); }
Point<T> translated(const Point<T>& delta) const
{
Point<T> point = *this;
point.move_by(delta);
point.translate_by(delta);
return point;
}
Point<T> translated(T dx, T dy) const
{
Point<T> point = *this;
point.move_by(dx, dy);
point.translate_by(dx, dy);
return point;
}
Point<T> translated(T dboth) const
{
Point<T> point = *this;
point.move_by(dboth, dboth);
point.translate_by(dboth, dboth);
return point;
}
Point<T> scaled(const Point<T>& delta) const
{
Point<T> point = *this;
point.scale_by(delta);
return point;
}
Point<T> scaled(T sx, T sy) const
{
Point<T> point = *this;
point.scale_by(sx, sy);
return point;
}
Point<T> transformed(const AffineTransform& transform) const
{
Point<T> point = *this;
point.transform_by(transform);
return point;
}
@ -87,6 +121,11 @@ public:
return point;
}
Point<T> moved_left(T amount) const { return { x() - amount, y() }; }
Point<T> moved_right(T amount) const { return { x() + amount, y() }; }
Point<T> moved_up(T amount) const { return { x(), y() - amount }; }
Point<T> moved_down(T amount) const { return { x(), y() + amount }; }
template<class U>
bool operator==(const Point<U>& other) const
{
@ -137,8 +176,6 @@ public:
return *this;
}
bool is_null() const { return !m_x && !m_y; }
T primary_offset_for_orientation(Orientation orientation) const
{
return orientation == Orientation::Vertical ? y() : x();

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/Format.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Orientation.h>
#include <LibGfx/Point.h>
#include <LibGfx/Size.h>
@ -24,7 +25,7 @@ T abst(T value)
template<typename T>
class Rect {
public:
Rect() { }
Rect() = default;
Rect(T x, T y, T width, T height)
: m_location(x, y)
@ -59,37 +60,47 @@ public:
{
}
bool is_null() const
{
return width() == 0 && height() == 0;
}
[[nodiscard]] ALWAYS_INLINE T x() const { return location().x(); }
[[nodiscard]] ALWAYS_INLINE T y() const { return location().y(); }
[[nodiscard]] ALWAYS_INLINE T width() const { return m_size.width(); }
[[nodiscard]] ALWAYS_INLINE T height() const { return m_size.height(); }
bool is_empty() const
{
return width() <= 0 || height() <= 0;
}
ALWAYS_INLINE void set_x(T x) { m_location.set_x(x); }
ALWAYS_INLINE void set_y(T y) { m_location.set_y(y); }
ALWAYS_INLINE void set_width(T width) { m_size.set_width(width); }
ALWAYS_INLINE void set_height(T height) { m_size.set_height(height); }
void move_by(T dx, T dy)
{
m_location.move_by(dx, dy);
}
[[nodiscard]] ALWAYS_INLINE const Point<T>& location() const { return m_location; }
[[nodiscard]] ALWAYS_INLINE const Size<T>& size() const { return m_size; }
void move_by(const Point<T>& delta)
[[nodiscard]] ALWAYS_INLINE bool is_null() const { return width() == 0 && height() == 0; }
[[nodiscard]] ALWAYS_INLINE bool is_empty() const { return width() <= 0 || height() <= 0; }
ALWAYS_INLINE void translate_by(T dx, T dy) { m_location.translate_by(dx, dy); }
ALWAYS_INLINE void translate_by(T dboth) { m_location.translate_by(dboth); }
ALWAYS_INLINE void translate_by(const Point<T>& delta) { m_location.translate_by(delta); }
ALWAYS_INLINE void scale_by(T dx, T dy)
{
m_location.move_by(delta);
m_location.scale_by(dx, dy);
m_size.scale_by(dx, dy);
}
ALWAYS_INLINE void scale_by(T dboth) { scale_by(dboth, dboth); }
ALWAYS_INLINE void scale_by(const Point<T>& delta) { scale_by(delta.x(), delta.y()); }
void transform_by(const AffineTransform& transform) { *this = transform.map(*this); }
Point<T> center() const
{
return { x() + width() / 2, y() + height() / 2 };
}
void set_location(const Point<T>& location)
ALWAYS_INLINE void set_location(const Point<T>& location)
{
m_location = location;
}
void set_size(const Size<T>& size)
ALWAYS_INLINE void set_size(const Size<T>& size)
{
m_size = size;
}
@ -134,6 +145,41 @@ public:
set_height(height() - size.height());
}
Rect<T> translated(T dx, T dy) const
{
Rect<T> rect = *this;
rect.translate_by(dx, dy);
return rect;
}
Rect<T> translated(const Point<T>& delta) const
{
Rect<T> rect = *this;
rect.translate_by(delta);
return rect;
}
Rect<T> scaled(T sx, T sy) const
{
Rect<T> rect = *this;
rect.scale_by(sx, sy);
return rect;
}
Rect<T> scaled(const Point<T>& s) const
{
Rect<T> rect = *this;
rect.scale_by(s);
return rect;
}
Rect<T> transformed(const AffineTransform& transform) const
{
Rect<T> rect = *this;
rect.transform_by(transform);
return rect;
}
Rect<T> shrunken(T w, T h) const
{
Rect<T> rect = *this;
@ -162,20 +208,6 @@ public:
return rect;
}
Rect<T> translated(T dx, T dy) const
{
Rect<T> rect = *this;
rect.move_by(dx, dy);
return rect;
}
Rect<T> translated(const Point<T>& delta) const
{
Rect<T> rect = *this;
rect.move_by(delta);
return rect;
}
Rect<T> take_from_right(T w)
{
if (w > width())
@ -235,7 +267,7 @@ public:
return x >= m_location.x() && x <= right() && y >= m_location.y() && y <= bottom();
}
bool contains(const Point<T>& point) const
ALWAYS_INLINE bool contains(const Point<T>& point) const
{
return contains(point.x(), point.y());
}
@ -260,15 +292,15 @@ public:
return have_any;
}
int primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); }
void set_primary_offset_for_orientation(Orientation orientation, int value) { m_location.set_primary_offset_for_orientation(orientation, value); }
int secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); }
void set_secondary_offset_for_orientation(Orientation orientation, int value) { m_location.set_secondary_offset_for_orientation(orientation, value); }
ALWAYS_INLINE int primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); }
ALWAYS_INLINE void set_primary_offset_for_orientation(Orientation orientation, int value) { m_location.set_primary_offset_for_orientation(orientation, value); }
ALWAYS_INLINE int secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); }
ALWAYS_INLINE void set_secondary_offset_for_orientation(Orientation orientation, int value) { m_location.set_secondary_offset_for_orientation(orientation, value); }
int primary_size_for_orientation(Orientation orientation) const { return m_size.primary_size_for_orientation(orientation); }
int secondary_size_for_orientation(Orientation orientation) const { return m_size.secondary_size_for_orientation(orientation); }
void set_primary_size_for_orientation(Orientation orientation, int value) { m_size.set_primary_size_for_orientation(orientation, value); }
void set_secondary_size_for_orientation(Orientation orientation, int value) { m_size.set_secondary_size_for_orientation(orientation, value); }
ALWAYS_INLINE int primary_size_for_orientation(Orientation orientation) const { return m_size.primary_size_for_orientation(orientation); }
ALWAYS_INLINE int secondary_size_for_orientation(Orientation orientation) const { return m_size.secondary_size_for_orientation(orientation); }
ALWAYS_INLINE void set_primary_size_for_orientation(Orientation orientation, int value) { m_size.set_primary_size_for_orientation(orientation, value); }
ALWAYS_INLINE void set_secondary_size_for_orientation(Orientation orientation, int value) { m_size.set_secondary_size_for_orientation(orientation, value); }
T first_edge_for_orientation(Orientation orientation) const
{
@ -284,27 +316,27 @@ public:
return right();
}
T left() const { return x(); }
T right() const { return x() + width() - 1; }
T top() const { return y(); }
T bottom() const { return y() + height() - 1; }
[[nodiscard]] ALWAYS_INLINE T left() const { return x(); }
[[nodiscard]] ALWAYS_INLINE T right() const { return x() + width() - 1; }
[[nodiscard]] ALWAYS_INLINE T top() const { return y(); }
[[nodiscard]] ALWAYS_INLINE T bottom() const { return y() + height() - 1; }
void set_left(T left)
ALWAYS_INLINE void set_left(T left)
{
set_x(left);
}
void set_top(T top)
ALWAYS_INLINE void set_top(T top)
{
set_y(top);
}
void set_right(T right)
ALWAYS_INLINE void set_right(T right)
{
set_width(right - x() + 1);
}
void set_bottom(T bottom)
ALWAYS_INLINE void set_bottom(T bottom)
{
set_height(bottom - y() + 1);
}
@ -312,13 +344,13 @@ public:
void set_right_without_resize(T new_right)
{
int delta = new_right - right();
move_by(delta, 0);
translate_by(delta, 0);
}
void set_bottom_without_resize(T new_bottom)
{
int delta = new_bottom - bottom();
move_by(0, delta);
translate_by(0, delta);
}
bool intersects_vertically(const Rect<T>& other) const
@ -365,19 +397,6 @@ public:
return IterationDecision::Continue;
}
T x() const { return location().x(); }
T y() const { return location().y(); }
T width() const { return m_size.width(); }
T height() const { return m_size.height(); }
void set_x(T x) { m_location.set_x(x); }
void set_y(T y) { m_location.set_y(y); }
void set_width(T width) { m_size.set_width(width); }
void set_height(T height) { m_size.set_height(height); }
const Point<T>& location() const { return m_location; }
const Size<T>& size() const { return m_size; }
Vector<Rect<T>, 4> shatter(const Rect<T>& hammer) const;
template<class U>
@ -415,7 +434,7 @@ public:
return r;
}
Rect<T> intersected(const Rect<T>& other) const
ALWAYS_INLINE Rect<T> intersected(const Rect<T>& other) const
{
return intersection(*this, other);
}
@ -446,7 +465,7 @@ public:
}
template<typename U>
Rect<U> to() const
ALWAYS_INLINE Rect<U> to_type() const
{
return Rect<U>(*this);
}

View file

@ -8,6 +8,7 @@
#include <AK/Format.h>
#include <LibGfx/Orientation.h>
#include <LibGfx/Point.h>
#include <LibIPC/Forward.h>
namespace Gfx {
@ -15,7 +16,7 @@ namespace Gfx {
template<typename T>
class Size {
public:
Size() { }
Size() = default;
Size(T w, T h)
: m_width(w)
@ -37,16 +38,54 @@ public:
{
}
bool is_null() const { return !m_width && !m_height; }
bool is_empty() const { return m_width <= 0 || m_height <= 0; }
[[nodiscard]] ALWAYS_INLINE T width() const { return m_width; }
[[nodiscard]] ALWAYS_INLINE T height() const { return m_height; }
[[nodiscard]] ALWAYS_INLINE T area() const { return width() * height(); }
T width() const { return m_width; }
T height() const { return m_height; }
ALWAYS_INLINE void set_width(T w) { m_width = w; }
ALWAYS_INLINE void set_height(T h) { m_height = h; }
T area() const { return width() * height(); }
[[nodiscard]] ALWAYS_INLINE bool is_null() const { return !m_width && !m_height; }
[[nodiscard]] ALWAYS_INLINE bool is_empty() const { return m_width <= 0 || m_height <= 0; }
void set_width(T w) { m_width = w; }
void set_height(T h) { m_height = h; }
void scale_by(T dx, T dy)
{
m_width *= dx;
m_height *= dy;
}
void transform_by(const AffineTransform& transform) { *this = transform.map(*this); }
ALWAYS_INLINE void scale_by(T dboth) { scale_by(dboth, dboth); }
ALWAYS_INLINE void scale_by(const Point<T>& s) { scale_by(s.x(), s.y()); }
Size scaled_by(T dx, T dy) const
{
Size<T> size = *this;
size.scale_by(dx, dy);
return size;
}
Size scaled_by(T dboth) const
{
Size<T> size = *this;
size.scale_by(dboth);
return size;
}
Size scaled_by(const Point<T>& s) const
{
Size<T> size = *this;
size.scale_by(s);
return size;
}
Size transformed_by(const AffineTransform& transform) const
{
Size<T> size = *this;
size.transform_by(transform);
return size;
}
template<typename U>
bool contains(const Size<U>& other) const
@ -118,7 +157,7 @@ public:
}
template<typename U>
Size<U> to_type() const
ALWAYS_INLINE Size<U> to_type() const
{
return Size<U>(*this);
}