2020-04-16 21:03:17 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-16 21:03:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2020-05-08 10:03:01 +04:30
|
|
|
#include <AK/HashMap.h>
|
|
|
|
|
#include <AK/Optional.h>
|
2020-04-16 21:03:17 +02:00
|
|
|
#include <AK/Vector.h>
|
2023-11-04 21:41:24 +00:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2020-04-16 21:03:17 +02:00
|
|
|
#include <LibGfx/Forward.h>
|
2023-06-03 23:40:03 +01:00
|
|
|
#include <LibGfx/Line.h>
|
2020-09-18 09:49:51 +02:00
|
|
|
#include <LibGfx/Point.h>
|
2020-10-05 16:22:24 -07:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-04-16 21:03:17 +02:00
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
2020-07-21 23:46:15 -07:00
|
|
|
class Segment : public RefCounted<Segment> {
|
2020-04-16 21:03:17 +02:00
|
|
|
public:
|
2020-07-21 23:46:15 -07:00
|
|
|
enum class Type {
|
|
|
|
|
Invalid,
|
|
|
|
|
MoveTo,
|
|
|
|
|
LineTo,
|
|
|
|
|
QuadraticBezierCurveTo,
|
2021-09-17 18:42:30 +04:30
|
|
|
CubicBezierCurveTo,
|
2020-04-16 21:03:17 +02:00
|
|
|
};
|
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
Segment(FloatPoint point)
|
2020-07-21 23:46:15 -07:00
|
|
|
: m_point(point)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~Segment() = default;
|
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
FloatPoint point() const { return m_point; }
|
2020-07-21 23:46:15 -07:00
|
|
|
virtual Type type() const = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
FloatPoint m_point;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MoveSegment final : public Segment {
|
|
|
|
|
public:
|
2022-12-06 20:57:07 +00:00
|
|
|
MoveSegment(FloatPoint point)
|
2020-07-21 23:46:15 -07:00
|
|
|
: Segment(point)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual Type type() const override { return Segment::Type::MoveTo; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class LineSegment final : public Segment {
|
|
|
|
|
public:
|
2022-12-06 20:57:07 +00:00
|
|
|
LineSegment(FloatPoint point)
|
2020-07-21 23:46:15 -07:00
|
|
|
: Segment(point)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~LineSegment() override = default;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual Type type() const override { return Segment::Type::LineTo; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QuadraticBezierCurveSegment final : public Segment {
|
|
|
|
|
public:
|
2022-12-06 20:57:07 +00:00
|
|
|
QuadraticBezierCurveSegment(FloatPoint point, FloatPoint through)
|
2020-07-21 23:46:15 -07:00
|
|
|
: Segment(point)
|
|
|
|
|
, m_through(through)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~QuadraticBezierCurveSegment() override = default;
|
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
FloatPoint through() const { return m_through; }
|
2020-07-21 23:46:15 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual Type type() const override { return Segment::Type::QuadraticBezierCurveTo; }
|
|
|
|
|
|
|
|
|
|
FloatPoint m_through;
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-17 18:42:30 +04:30
|
|
|
class CubicBezierCurveSegment final : public Segment {
|
|
|
|
|
public:
|
2022-12-06 20:57:07 +00:00
|
|
|
CubicBezierCurveSegment(FloatPoint point, FloatPoint through_0, FloatPoint through_1)
|
2021-09-17 18:42:30 +04:30
|
|
|
: Segment(point)
|
|
|
|
|
, m_through_0(through_0)
|
|
|
|
|
, m_through_1(through_1)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~CubicBezierCurveSegment() override = default;
|
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
FloatPoint through_0() const { return m_through_0; }
|
|
|
|
|
FloatPoint through_1() const { return m_through_1; }
|
2021-09-17 18:42:30 +04:30
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual Type type() const override { return Segment::Type::CubicBezierCurveTo; }
|
|
|
|
|
|
|
|
|
|
FloatPoint m_through_0;
|
|
|
|
|
FloatPoint m_through_1;
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-21 23:46:15 -07:00
|
|
|
class Path {
|
|
|
|
|
public:
|
2022-03-14 13:26:37 -06:00
|
|
|
Path() = default;
|
2020-04-16 21:03:17 +02:00
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
void move_to(FloatPoint point)
|
2020-04-16 21:03:17 +02:00
|
|
|
{
|
2020-07-21 23:46:15 -07:00
|
|
|
append_segment<MoveSegment>(point);
|
2020-04-16 21:03:17 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
void line_to(FloatPoint point)
|
2020-04-16 21:03:17 +02:00
|
|
|
{
|
2020-07-21 23:46:15 -07:00
|
|
|
append_segment<LineSegment>(point);
|
2020-05-06 11:55:12 +04:30
|
|
|
invalidate_split_lines();
|
2020-04-16 21:03:17 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-11 17:47:39 +00:00
|
|
|
void horizontal_line_to(float x)
|
|
|
|
|
{
|
|
|
|
|
float previous_y = 0;
|
|
|
|
|
if (!m_segments.is_empty())
|
2023-03-06 14:17:01 +01:00
|
|
|
previous_y = m_segments.last()->point().y();
|
2022-02-11 17:47:39 +00:00
|
|
|
line_to({ x, previous_y });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vertical_line_to(float y)
|
|
|
|
|
{
|
|
|
|
|
float previous_x = 0;
|
|
|
|
|
if (!m_segments.is_empty())
|
2023-03-06 14:17:01 +01:00
|
|
|
previous_x = m_segments.last()->point().x();
|
2022-02-11 17:47:39 +00:00
|
|
|
line_to({ previous_x, y });
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
void quadratic_bezier_curve_to(FloatPoint through, FloatPoint point)
|
2020-05-05 05:45:17 +04:30
|
|
|
{
|
2020-07-21 23:46:15 -07:00
|
|
|
append_segment<QuadraticBezierCurveSegment>(point, through);
|
|
|
|
|
invalidate_split_lines();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
void cubic_bezier_curve_to(FloatPoint c1, FloatPoint c2, FloatPoint p2)
|
2021-09-17 18:42:30 +04:30
|
|
|
{
|
|
|
|
|
append_segment<CubicBezierCurveSegment>(p2, c1, c2);
|
|
|
|
|
invalidate_split_lines();
|
|
|
|
|
}
|
2021-09-15 19:54:33 +02:00
|
|
|
|
2023-07-16 00:09:11 +01:00
|
|
|
void elliptical_arc_to(FloatPoint point, FloatSize radii, float x_axis_rotation, bool large_arc, bool sweep);
|
2022-12-06 20:57:07 +00:00
|
|
|
void arc_to(FloatPoint point, float radius, bool large_arc, bool sweep)
|
2021-04-15 04:03:59 +04:30
|
|
|
{
|
|
|
|
|
elliptical_arc_to(point, { radius, radius }, 0, large_arc, sweep);
|
|
|
|
|
}
|
2020-05-05 05:45:17 +04:30
|
|
|
|
2023-11-04 21:41:24 +00:00
|
|
|
void text(Utf8View, Font const&);
|
|
|
|
|
|
2023-09-17 05:15:21 -07:00
|
|
|
FloatPoint last_point();
|
|
|
|
|
|
2020-04-16 21:03:17 +02:00
|
|
|
void close();
|
2020-05-10 01:46:09 +04:30
|
|
|
void close_all_subpaths();
|
2020-04-16 21:03:17 +02:00
|
|
|
|
2023-03-06 14:17:01 +01:00
|
|
|
Vector<NonnullRefPtr<Segment const>> const& segments() const { return m_segments; }
|
2023-06-03 23:40:03 +01:00
|
|
|
|
2021-09-17 13:35:17 +02:00
|
|
|
auto& split_lines() const
|
2020-05-06 11:55:12 +04:30
|
|
|
{
|
2020-10-05 16:22:24 -07:00
|
|
|
if (!m_split_lines.has_value()) {
|
2021-09-17 13:35:17 +02:00
|
|
|
const_cast<Path*>(this)->segmentize_path();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_split_lines.has_value());
|
2020-10-05 16:22:24 -07:00
|
|
|
}
|
2020-05-06 11:55:12 +04:30
|
|
|
return m_split_lines.value();
|
|
|
|
|
}
|
2020-04-16 21:03:17 +02:00
|
|
|
|
2021-05-24 08:25:53 -07:00
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
m_segments.clear();
|
|
|
|
|
m_split_lines.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 13:35:17 +02:00
|
|
|
Gfx::FloatRect const& bounding_box() const
|
2020-10-05 16:22:24 -07:00
|
|
|
{
|
|
|
|
|
if (!m_bounding_box.has_value()) {
|
2021-09-17 13:35:17 +02:00
|
|
|
const_cast<Path*>(this)->segmentize_path();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_bounding_box.has_value());
|
2020-10-05 16:22:24 -07:00
|
|
|
}
|
|
|
|
|
return m_bounding_box.value();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 13:59:48 +08:00
|
|
|
void append_path(Path const& path)
|
|
|
|
|
{
|
|
|
|
|
m_segments.ensure_capacity(m_segments.size() + path.m_segments.size());
|
|
|
|
|
for (auto const& segment : path.m_segments)
|
|
|
|
|
m_segments.unchecked_append(segment);
|
|
|
|
|
invalidate_split_lines();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 17:38:35 +01:00
|
|
|
Path copy_transformed(AffineTransform const&) const;
|
2023-02-27 18:22:03 +00:00
|
|
|
void add_path(Path const&);
|
2023-09-17 05:15:21 -07:00
|
|
|
void ensure_subpath(FloatPoint point);
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString to_byte_string() const;
|
2020-04-16 21:03:17 +02:00
|
|
|
|
2023-06-05 21:34:23 +01:00
|
|
|
Path stroke_to_fill(float thickness) const;
|
|
|
|
|
|
2023-12-18 01:03:45 +00:00
|
|
|
Path place_text_along(Utf8View text, Font const&) const;
|
|
|
|
|
|
2020-04-16 21:03:17 +02:00
|
|
|
private:
|
2023-07-15 15:59:33 +01:00
|
|
|
void approximate_elliptical_arc_with_cubic_beziers(FloatPoint center, FloatSize radii, float x_axis_rotation, float theta, float theta_delta);
|
|
|
|
|
|
2020-05-08 10:03:01 +04:30
|
|
|
void invalidate_split_lines()
|
|
|
|
|
{
|
2023-06-16 19:02:00 +01:00
|
|
|
m_bounding_box.clear();
|
2020-05-08 10:03:01 +04:30
|
|
|
m_split_lines.clear();
|
|
|
|
|
}
|
2020-05-06 11:55:12 +04:30
|
|
|
void segmentize_path();
|
|
|
|
|
|
2020-07-21 23:46:15 -07:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
|
void append_segment(Args&&... args)
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
m_segments.append(adopt_ref(*new T(forward<Args>(args)...)));
|
2020-07-21 23:46:15 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 14:17:01 +01:00
|
|
|
Vector<NonnullRefPtr<Segment const>> m_segments {};
|
2020-05-06 11:55:12 +04:30
|
|
|
|
2023-06-03 23:40:03 +01:00
|
|
|
Optional<Vector<FloatLine>> m_split_lines {};
|
2020-10-05 16:22:24 -07:00
|
|
|
Optional<Gfx::FloatRect> m_bounding_box;
|
2023-09-17 05:15:21 -07:00
|
|
|
bool m_need_new_subpath = { true };
|
2020-04-16 21:03:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|