Path.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/NonnullRefPtrVector.h>
  9. #include <AK/Optional.h>
  10. #include <AK/String.h>
  11. #include <AK/Vector.h>
  12. #include <LibGfx/Forward.h>
  13. #include <LibGfx/Point.h>
  14. #include <LibGfx/Rect.h>
  15. namespace Gfx {
  16. class Segment : public RefCounted<Segment> {
  17. public:
  18. enum class Type {
  19. Invalid,
  20. MoveTo,
  21. LineTo,
  22. QuadraticBezierCurveTo,
  23. CubicBezierCurveTo,
  24. EllipticalArcTo,
  25. };
  26. Segment(const FloatPoint& point)
  27. : m_point(point)
  28. {
  29. }
  30. virtual ~Segment() = default;
  31. const FloatPoint& point() const { return m_point; }
  32. virtual Type type() const = 0;
  33. protected:
  34. FloatPoint m_point;
  35. };
  36. class MoveSegment final : public Segment {
  37. public:
  38. MoveSegment(const FloatPoint& point)
  39. : Segment(point)
  40. {
  41. }
  42. private:
  43. virtual Type type() const override { return Segment::Type::MoveTo; }
  44. };
  45. class LineSegment final : public Segment {
  46. public:
  47. LineSegment(const FloatPoint& point)
  48. : Segment(point)
  49. {
  50. }
  51. virtual ~LineSegment() override = default;
  52. private:
  53. virtual Type type() const override { return Segment::Type::LineTo; }
  54. };
  55. class QuadraticBezierCurveSegment final : public Segment {
  56. public:
  57. QuadraticBezierCurveSegment(const FloatPoint& point, const FloatPoint& through)
  58. : Segment(point)
  59. , m_through(through)
  60. {
  61. }
  62. virtual ~QuadraticBezierCurveSegment() override = default;
  63. const FloatPoint& through() const { return m_through; }
  64. private:
  65. virtual Type type() const override { return Segment::Type::QuadraticBezierCurveTo; }
  66. FloatPoint m_through;
  67. };
  68. class CubicBezierCurveSegment final : public Segment {
  69. public:
  70. CubicBezierCurveSegment(const FloatPoint& point, const FloatPoint& through_0, const FloatPoint& through_1)
  71. : Segment(point)
  72. , m_through_0(through_0)
  73. , m_through_1(through_1)
  74. {
  75. }
  76. virtual ~CubicBezierCurveSegment() override = default;
  77. const FloatPoint& through_0() const { return m_through_0; }
  78. const FloatPoint& through_1() const { return m_through_1; }
  79. private:
  80. virtual Type type() const override { return Segment::Type::CubicBezierCurveTo; }
  81. FloatPoint m_through_0;
  82. FloatPoint m_through_1;
  83. };
  84. class EllipticalArcSegment final : public Segment {
  85. public:
  86. EllipticalArcSegment(const FloatPoint& point, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta)
  87. : Segment(point)
  88. , m_center(center)
  89. , m_radii(radii)
  90. , m_x_axis_rotation(x_axis_rotation)
  91. , m_theta_1(theta_1)
  92. , m_theta_delta(theta_delta)
  93. {
  94. }
  95. virtual ~EllipticalArcSegment() override = default;
  96. const FloatPoint& center() const { return m_center; }
  97. const FloatPoint& radii() const { return m_radii; }
  98. float x_axis_rotation() const { return m_x_axis_rotation; }
  99. float theta_1() const { return m_theta_1; }
  100. float theta_delta() const { return m_theta_delta; }
  101. private:
  102. virtual Type type() const override { return Segment::Type::EllipticalArcTo; }
  103. FloatPoint m_center;
  104. FloatPoint m_radii;
  105. float m_x_axis_rotation;
  106. float m_theta_1;
  107. float m_theta_delta;
  108. };
  109. class Path {
  110. public:
  111. Path() { }
  112. void move_to(const FloatPoint& point)
  113. {
  114. append_segment<MoveSegment>(point);
  115. }
  116. void line_to(const FloatPoint& point)
  117. {
  118. append_segment<LineSegment>(point);
  119. invalidate_split_lines();
  120. }
  121. void quadratic_bezier_curve_to(const FloatPoint& through, const FloatPoint& point)
  122. {
  123. append_segment<QuadraticBezierCurveSegment>(point, through);
  124. invalidate_split_lines();
  125. }
  126. void cubic_bezier_curve_to(FloatPoint const& c1, FloatPoint const& c2, FloatPoint const& p2)
  127. {
  128. append_segment<CubicBezierCurveSegment>(p2, c1, c2);
  129. invalidate_split_lines();
  130. }
  131. void elliptical_arc_to(const FloatPoint& point, const FloatPoint& radii, double x_axis_rotation, bool large_arc, bool sweep);
  132. void arc_to(const FloatPoint& point, float radius, bool large_arc, bool sweep)
  133. {
  134. elliptical_arc_to(point, { radius, radius }, 0, large_arc, sweep);
  135. }
  136. // Note: This does not do any sanity checks!
  137. void elliptical_arc_to(const FloatPoint& endpoint, const FloatPoint& center, const FloatPoint& radii, double x_axis_rotation, double theta, double theta_delta)
  138. {
  139. append_segment<EllipticalArcSegment>(
  140. endpoint,
  141. center,
  142. radii,
  143. x_axis_rotation,
  144. theta,
  145. theta_delta);
  146. invalidate_split_lines();
  147. }
  148. void close();
  149. void close_all_subpaths();
  150. struct SplitLineSegment {
  151. FloatPoint from, to;
  152. float inverse_slope;
  153. float x_of_minimum_y;
  154. float maximum_y;
  155. float minimum_y;
  156. float x;
  157. };
  158. const NonnullRefPtrVector<Segment>& segments() const { return m_segments; }
  159. auto& split_lines() const
  160. {
  161. if (!m_split_lines.has_value()) {
  162. const_cast<Path*>(this)->segmentize_path();
  163. VERIFY(m_split_lines.has_value());
  164. }
  165. return m_split_lines.value();
  166. }
  167. void clear()
  168. {
  169. m_segments.clear();
  170. m_split_lines.clear();
  171. }
  172. Gfx::FloatRect const& bounding_box() const
  173. {
  174. if (!m_bounding_box.has_value()) {
  175. const_cast<Path*>(this)->segmentize_path();
  176. VERIFY(m_bounding_box.has_value());
  177. }
  178. return m_bounding_box.value();
  179. }
  180. String to_string() const;
  181. private:
  182. void invalidate_split_lines()
  183. {
  184. m_split_lines.clear();
  185. }
  186. void segmentize_path();
  187. template<typename T, typename... Args>
  188. void append_segment(Args&&... args)
  189. {
  190. m_segments.append(adopt_ref(*new T(forward<Args>(args)...)));
  191. }
  192. NonnullRefPtrVector<Segment> m_segments {};
  193. Optional<Vector<SplitLineSegment>> m_split_lines {};
  194. Optional<Gfx::FloatRect> m_bounding_box;
  195. };
  196. }