Path.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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(FloatPoint const& point)
  27. : m_point(point)
  28. {
  29. }
  30. virtual ~Segment() = default;
  31. FloatPoint const& 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(FloatPoint const& 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(FloatPoint const& 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(FloatPoint const& point, FloatPoint const& through)
  58. : Segment(point)
  59. , m_through(through)
  60. {
  61. }
  62. virtual ~QuadraticBezierCurveSegment() override = default;
  63. FloatPoint const& 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(FloatPoint const& point, FloatPoint const& through_0, FloatPoint const& 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. FloatPoint const& through_0() const { return m_through_0; }
  78. FloatPoint const& 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(FloatPoint const& point, FloatPoint const& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, bool large_arc, bool sweep)
  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. , m_large_arc(large_arc)
  94. , m_sweep(sweep)
  95. {
  96. }
  97. virtual ~EllipticalArcSegment() override = default;
  98. FloatPoint const& center() const { return m_center; }
  99. FloatPoint const& radii() const { return m_radii; }
  100. float x_axis_rotation() const { return m_x_axis_rotation; }
  101. float theta_1() const { return m_theta_1; }
  102. float theta_delta() const { return m_theta_delta; }
  103. bool large_arc() const { return m_large_arc; }
  104. bool sweep() const { return m_sweep; }
  105. private:
  106. virtual Type type() const override { return Segment::Type::EllipticalArcTo; }
  107. FloatPoint m_center;
  108. FloatPoint m_radii;
  109. float m_x_axis_rotation;
  110. float m_theta_1;
  111. float m_theta_delta;
  112. bool m_large_arc;
  113. bool m_sweep;
  114. };
  115. class Path {
  116. public:
  117. Path() = default;
  118. void move_to(FloatPoint const& point)
  119. {
  120. append_segment<MoveSegment>(point);
  121. }
  122. void line_to(FloatPoint const& point)
  123. {
  124. append_segment<LineSegment>(point);
  125. invalidate_split_lines();
  126. }
  127. void horizontal_line_to(float x)
  128. {
  129. float previous_y = 0;
  130. if (!m_segments.is_empty())
  131. previous_y = m_segments.last().point().y();
  132. line_to({ x, previous_y });
  133. }
  134. void vertical_line_to(float y)
  135. {
  136. float previous_x = 0;
  137. if (!m_segments.is_empty())
  138. previous_x = m_segments.last().point().x();
  139. line_to({ previous_x, y });
  140. }
  141. void quadratic_bezier_curve_to(FloatPoint const& through, FloatPoint const& point)
  142. {
  143. append_segment<QuadraticBezierCurveSegment>(point, through);
  144. invalidate_split_lines();
  145. }
  146. void cubic_bezier_curve_to(FloatPoint const& c1, FloatPoint const& c2, FloatPoint const& p2)
  147. {
  148. append_segment<CubicBezierCurveSegment>(p2, c1, c2);
  149. invalidate_split_lines();
  150. }
  151. void elliptical_arc_to(FloatPoint const& point, FloatPoint const& radii, double x_axis_rotation, bool large_arc, bool sweep);
  152. void arc_to(FloatPoint const& point, float radius, bool large_arc, bool sweep)
  153. {
  154. elliptical_arc_to(point, { radius, radius }, 0, large_arc, sweep);
  155. }
  156. // Note: This does not do any sanity checks!
  157. void elliptical_arc_to(FloatPoint const& endpoint, FloatPoint const& center, FloatPoint const& radii, double x_axis_rotation, double theta, double theta_delta, bool large_arc, bool sweep)
  158. {
  159. append_segment<EllipticalArcSegment>(
  160. endpoint,
  161. center,
  162. radii,
  163. x_axis_rotation,
  164. theta,
  165. theta_delta,
  166. large_arc,
  167. sweep);
  168. invalidate_split_lines();
  169. }
  170. void close();
  171. void close_all_subpaths();
  172. struct SplitLineSegment {
  173. FloatPoint from, to;
  174. float inverse_slope;
  175. float x_of_minimum_y;
  176. float maximum_y;
  177. float minimum_y;
  178. float x;
  179. };
  180. NonnullRefPtrVector<Segment> const& segments() const { return m_segments; }
  181. auto& split_lines() const
  182. {
  183. if (!m_split_lines.has_value()) {
  184. const_cast<Path*>(this)->segmentize_path();
  185. VERIFY(m_split_lines.has_value());
  186. }
  187. return m_split_lines.value();
  188. }
  189. void clear()
  190. {
  191. m_segments.clear();
  192. m_split_lines.clear();
  193. }
  194. Gfx::FloatRect const& bounding_box() const
  195. {
  196. if (!m_bounding_box.has_value()) {
  197. const_cast<Path*>(this)->segmentize_path();
  198. VERIFY(m_bounding_box.has_value());
  199. }
  200. return m_bounding_box.value();
  201. }
  202. String to_string() const;
  203. private:
  204. void invalidate_split_lines()
  205. {
  206. m_split_lines.clear();
  207. }
  208. void segmentize_path();
  209. template<typename T, typename... Args>
  210. void append_segment(Args&&... args)
  211. {
  212. m_segments.append(adopt_ref(*new T(forward<Args>(args)...)));
  213. }
  214. NonnullRefPtrVector<Segment> m_segments {};
  215. Optional<Vector<SplitLineSegment>> m_split_lines {};
  216. Optional<Gfx::FloatRect> m_bounding_box;
  217. };
  218. }