mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 10:18:15 +09:00
LibGfx: Add FloatPoint methods
Adds some conversion constructors, as well as the missing arithmetic operations.
This commit is contained in:
parent
5985eac81d
commit
9cce7f57dd
Notes:
sideshowbarker
2024-07-19 04:37:02 +09:00
Author: https://github.com/mattco98
Commit: 9cce7f57dd
Pull-request: https://github.com/SerenityOS/serenity/pull/2861
4 changed files with 54 additions and 14 deletions
|
@ -30,6 +30,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibGfx/Orientation.h>
|
#include <LibGfx/Orientation.h>
|
||||||
#include <LibGfx/Point.h>
|
#include <LibGfx/Point.h>
|
||||||
|
#include <LibM/math.h>
|
||||||
|
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
|
||||||
|
@ -38,12 +39,25 @@ class FloatRect;
|
||||||
class FloatPoint {
|
class FloatPoint {
|
||||||
public:
|
public:
|
||||||
FloatPoint() { }
|
FloatPoint() { }
|
||||||
|
|
||||||
|
FloatPoint(int x, int y)
|
||||||
|
: m_x(x)
|
||||||
|
, m_y(y)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
FloatPoint(float x, float y)
|
FloatPoint(float x, float y)
|
||||||
: m_x(x)
|
: m_x(x)
|
||||||
, m_y(y)
|
, m_y(y)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FloatPoint(double x, double y)
|
||||||
|
: m_x(x)
|
||||||
|
, m_y(y)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
explicit FloatPoint(const IntPoint& other)
|
explicit FloatPoint(const IntPoint& other)
|
||||||
: m_x(other.x())
|
: m_x(other.x())
|
||||||
, m_y(other.y())
|
, m_y(other.y())
|
||||||
|
@ -96,6 +110,7 @@ public:
|
||||||
|
|
||||||
FloatPoint operator-() const { return { -m_x, -m_y }; }
|
FloatPoint operator-() const { return { -m_x, -m_y }; }
|
||||||
|
|
||||||
|
FloatPoint operator-(float number) const { return { m_x - number, m_y - number }; }
|
||||||
FloatPoint operator-(const FloatPoint& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
|
FloatPoint operator-(const FloatPoint& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
|
||||||
FloatPoint& operator-=(const FloatPoint& other)
|
FloatPoint& operator-=(const FloatPoint& other)
|
||||||
{
|
{
|
||||||
|
@ -104,14 +119,24 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FloatPoint operator+(float number) const { return { m_x + number, m_y + number }; }
|
||||||
|
FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
|
||||||
FloatPoint& operator+=(const FloatPoint& other)
|
FloatPoint& operator+=(const FloatPoint& other)
|
||||||
{
|
{
|
||||||
m_x += other.m_x;
|
m_x += other.m_x;
|
||||||
m_y += other.m_y;
|
m_y += other.m_y;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
|
|
||||||
|
|
||||||
|
FloatPoint operator*(float factor) const { return { m_x * factor, m_y * factor }; }
|
||||||
|
FloatPoint& operator*=(float factor)
|
||||||
|
{
|
||||||
|
m_x *= factor;
|
||||||
|
m_y *= factor;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
FloatPoint operator/(float factor) const { return { m_x / factor, m_y / factor }; }
|
||||||
FloatPoint& operator/=(float factor)
|
FloatPoint& operator/=(float factor)
|
||||||
{
|
{
|
||||||
m_x /= factor;
|
m_x /= factor;
|
||||||
|
@ -119,6 +144,13 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float distance_from(const FloatPoint& other) const
|
||||||
|
{
|
||||||
|
if (*this == other)
|
||||||
|
return 0;
|
||||||
|
return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f));
|
||||||
|
}
|
||||||
|
|
||||||
String to_string() const { return String::format("[%g,%g]", x(), y()); }
|
String to_string() const { return String::format("[%g,%g]", x(), y()); }
|
||||||
|
|
||||||
bool is_null() const { return !m_x && !m_y; }
|
bool is_null() const { return !m_x && !m_y; }
|
||||||
|
|
|
@ -1565,7 +1565,7 @@ void Painter::fill_path(Path& path, Color color, WindingRule winding_rule)
|
||||||
#ifdef FILL_PATH_DEBUG
|
#ifdef FILL_PATH_DEBUG
|
||||||
size_t i { 0 };
|
size_t i { 0 };
|
||||||
for (auto& segment : segments)
|
for (auto& segment : segments)
|
||||||
draw_line(IntPoint(segment.from.x(), segment.from.y()), IntPoint(segment.to.x(), segment.to.y()), Color::from_hsv(++i / segments.size() * 255, 255, 255), 1);
|
draw_line(segment.from, segment.to, Color::from_hsv(++i / segments.size() * 255, 255, 255), 1);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -434,25 +434,29 @@ void HTMLPathElement::paint(const SvgPaintingContext& context, Gfx::Painter& pai
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
switch (instruction.type) {
|
switch (instruction.type) {
|
||||||
case PathInstructionType::Move:
|
case PathInstructionType::Move: {
|
||||||
|
Gfx::FloatPoint point = { data[0], data[1] };
|
||||||
if (absolute) {
|
if (absolute) {
|
||||||
path.move_to({ data[0], data[1] });
|
path.move_to(point);
|
||||||
} else {
|
} else {
|
||||||
ASSERT(!path.segments().is_empty());
|
ASSERT(!path.segments().is_empty());
|
||||||
path.move_to(Gfx::FloatPoint { data[0], data[1] } + path.segments().last().point());
|
path.move_to(point + path.segments().last().point());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case PathInstructionType::ClosePath:
|
case PathInstructionType::ClosePath:
|
||||||
path.close();
|
path.close();
|
||||||
break;
|
break;
|
||||||
case PathInstructionType::Line:
|
case PathInstructionType::Line: {
|
||||||
|
Gfx::FloatPoint point = { data[0], data[1] };
|
||||||
if (absolute) {
|
if (absolute) {
|
||||||
path.line_to({ data[0], data[1] });
|
path.line_to(point);
|
||||||
} else {
|
} else {
|
||||||
ASSERT(!path.segments().is_empty());
|
ASSERT(!path.segments().is_empty());
|
||||||
path.line_to(Gfx::FloatPoint { data[0], data[1] } + path.segments().last().point());
|
path.line_to(point + path.segments().last().point());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case PathInstructionType::HorizontalLine: {
|
case PathInstructionType::HorizontalLine: {
|
||||||
ASSERT(!path.segments().is_empty());
|
ASSERT(!path.segments().is_empty());
|
||||||
auto last_point = path.segments().last().point();
|
auto last_point = path.segments().last().point();
|
||||||
|
@ -467,9 +471,9 @@ void HTMLPathElement::paint(const SvgPaintingContext& context, Gfx::Painter& pai
|
||||||
ASSERT(!path.segments().is_empty());
|
ASSERT(!path.segments().is_empty());
|
||||||
auto last_point = path.segments().last().point();
|
auto last_point = path.segments().last().point();
|
||||||
if (absolute) {
|
if (absolute) {
|
||||||
path.line_to(Gfx::FloatPoint{ last_point.x(), data[0] });
|
path.line_to(Gfx::FloatPoint { last_point.x(), data[0] });
|
||||||
} else {
|
} else {
|
||||||
path.line_to(Gfx::FloatPoint{ last_point.x(), data[0] + last_point.y() });
|
path.line_to(Gfx::FloatPoint { last_point.x(), data[0] + last_point.y() });
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -560,15 +564,19 @@ void HTMLPathElement::paint(const SvgPaintingContext& context, Gfx::Painter& pai
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PathInstructionType::QuadraticBezierCurve:
|
case PathInstructionType::QuadraticBezierCurve: {
|
||||||
|
Gfx::FloatPoint through = { data[0], data[1] };
|
||||||
|
Gfx::FloatPoint point = { data[2], data[3] };
|
||||||
|
|
||||||
if (absolute) {
|
if (absolute) {
|
||||||
path.quadratic_bezier_curve_to({ data[0], data[1] }, { data[2], data[3] });
|
path.quadratic_bezier_curve_to(through, point);
|
||||||
} else {
|
} else {
|
||||||
ASSERT(!path.segments().is_empty());
|
ASSERT(!path.segments().is_empty());
|
||||||
auto last_point = path.segments().last().point();
|
auto last_point = path.segments().last().point();
|
||||||
path.quadratic_bezier_curve_to({ data[0] + last_point.x(), data[1] + last_point.y() }, { data[2] + last_point.x(), data[3] + last_point.y() });
|
path.quadratic_bezier_curve_to(through + last_point, point + last_point);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case PathInstructionType::Curve:
|
case PathInstructionType::Curve:
|
||||||
case PathInstructionType::SmoothCurve:
|
case PathInstructionType::SmoothCurve:
|
||||||
case PathInstructionType::SmoothQuadraticBezierCurve:
|
case PathInstructionType::SmoothQuadraticBezierCurve:
|
||||||
|
|
|
@ -42,7 +42,7 @@ void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length,
|
||||||
m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
|
m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
|
||||||
m_fragments.last().set_width(m_fragments.last().width() + width);
|
m_fragments.last().set_width(m_fragments.last().width() + width);
|
||||||
} else {
|
} else {
|
||||||
m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width, 0), Gfx::FloatSize(width, height)));
|
m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width, 0.0f), Gfx::FloatSize(width, height)));
|
||||||
}
|
}
|
||||||
m_width += width;
|
m_width += width;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue