Path.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/DeprecatedString.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/Optional.h>
  10. #include <AK/Vector.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibGfx/Point.h>
  13. #include <LibGfx/Rect.h>
  14. namespace Gfx {
  15. class Segment : public RefCounted<Segment> {
  16. public:
  17. enum class Type {
  18. Invalid,
  19. MoveTo,
  20. LineTo,
  21. QuadraticBezierCurveTo,
  22. CubicBezierCurveTo,
  23. EllipticalArcTo,
  24. };
  25. Segment(FloatPoint point)
  26. : m_point(point)
  27. {
  28. }
  29. virtual ~Segment() = default;
  30. FloatPoint point() const { return m_point; }
  31. virtual Type type() const = 0;
  32. protected:
  33. FloatPoint m_point;
  34. };
  35. class MoveSegment final : public Segment {
  36. public:
  37. MoveSegment(FloatPoint point)
  38. : Segment(point)
  39. {
  40. }
  41. private:
  42. virtual Type type() const override { return Segment::Type::MoveTo; }
  43. };
  44. class LineSegment final : public Segment {
  45. public:
  46. LineSegment(FloatPoint point)
  47. : Segment(point)
  48. {
  49. }
  50. virtual ~LineSegment() override = default;
  51. private:
  52. virtual Type type() const override { return Segment::Type::LineTo; }
  53. };
  54. class QuadraticBezierCurveSegment final : public Segment {
  55. public:
  56. QuadraticBezierCurveSegment(FloatPoint point, FloatPoint through)
  57. : Segment(point)
  58. , m_through(through)
  59. {
  60. }
  61. virtual ~QuadraticBezierCurveSegment() override = default;
  62. FloatPoint through() const { return m_through; }
  63. private:
  64. virtual Type type() const override { return Segment::Type::QuadraticBezierCurveTo; }
  65. FloatPoint m_through;
  66. };
  67. class CubicBezierCurveSegment final : public Segment {
  68. public:
  69. CubicBezierCurveSegment(FloatPoint point, FloatPoint through_0, FloatPoint through_1)
  70. : Segment(point)
  71. , m_through_0(through_0)
  72. , m_through_1(through_1)
  73. {
  74. }
  75. virtual ~CubicBezierCurveSegment() override = default;
  76. FloatPoint through_0() const { return m_through_0; }
  77. FloatPoint through_1() const { return m_through_1; }
  78. private:
  79. virtual Type type() const override { return Segment::Type::CubicBezierCurveTo; }
  80. FloatPoint m_through_0;
  81. FloatPoint m_through_1;
  82. };
  83. class EllipticalArcSegment final : public Segment {
  84. public:
  85. EllipticalArcSegment(FloatPoint point, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, bool large_arc, bool sweep)
  86. : Segment(point)
  87. , m_center(center)
  88. , m_radii(radii)
  89. , m_x_axis_rotation(x_axis_rotation)
  90. , m_theta_1(theta_1)
  91. , m_theta_delta(theta_delta)
  92. , m_large_arc(large_arc)
  93. , m_sweep(sweep)
  94. {
  95. }
  96. virtual ~EllipticalArcSegment() override = default;
  97. FloatPoint center() const { return m_center; }
  98. FloatSize radii() const { return m_radii; }
  99. float x_axis_rotation() const { return m_x_axis_rotation; }
  100. float theta_1() const { return m_theta_1; }
  101. float theta_delta() const { return m_theta_delta; }
  102. bool large_arc() const { return m_large_arc; }
  103. bool sweep() const { return m_sweep; }
  104. private:
  105. virtual Type type() const override { return Segment::Type::EllipticalArcTo; }
  106. FloatPoint m_center;
  107. FloatSize m_radii;
  108. float m_x_axis_rotation;
  109. float m_theta_1;
  110. float m_theta_delta;
  111. bool m_large_arc;
  112. bool m_sweep;
  113. };
  114. class Path {
  115. public:
  116. Path() = default;
  117. void move_to(FloatPoint point)
  118. {
  119. append_segment<MoveSegment>(point);
  120. }
  121. void line_to(FloatPoint point)
  122. {
  123. append_segment<LineSegment>(point);
  124. invalidate_split_lines();
  125. }
  126. void horizontal_line_to(float x)
  127. {
  128. float previous_y = 0;
  129. if (!m_segments.is_empty())
  130. previous_y = m_segments.last()->point().y();
  131. line_to({ x, previous_y });
  132. }
  133. void vertical_line_to(float y)
  134. {
  135. float previous_x = 0;
  136. if (!m_segments.is_empty())
  137. previous_x = m_segments.last()->point().x();
  138. line_to({ previous_x, y });
  139. }
  140. void quadratic_bezier_curve_to(FloatPoint through, FloatPoint point)
  141. {
  142. append_segment<QuadraticBezierCurveSegment>(point, through);
  143. invalidate_split_lines();
  144. }
  145. void cubic_bezier_curve_to(FloatPoint c1, FloatPoint c2, FloatPoint p2)
  146. {
  147. append_segment<CubicBezierCurveSegment>(p2, c1, c2);
  148. invalidate_split_lines();
  149. }
  150. void elliptical_arc_to(FloatPoint point, FloatSize radii, double x_axis_rotation, bool large_arc, bool sweep);
  151. void arc_to(FloatPoint point, float radius, bool large_arc, bool sweep)
  152. {
  153. elliptical_arc_to(point, { radius, radius }, 0, large_arc, sweep);
  154. }
  155. // Note: This does not do any sanity checks!
  156. void elliptical_arc_to(FloatPoint endpoint, FloatPoint center, FloatSize radii, double x_axis_rotation, double theta, double theta_delta, bool large_arc, bool sweep)
  157. {
  158. append_segment<EllipticalArcSegment>(
  159. endpoint,
  160. center,
  161. radii,
  162. x_axis_rotation,
  163. theta,
  164. theta_delta,
  165. large_arc,
  166. sweep);
  167. invalidate_split_lines();
  168. }
  169. void close();
  170. void close_all_subpaths();
  171. struct SplitLineSegment {
  172. FloatPoint from, to;
  173. float inverse_slope;
  174. float x_of_minimum_y;
  175. float maximum_y;
  176. float minimum_y;
  177. float x;
  178. };
  179. Vector<NonnullRefPtr<Segment const>> const& segments() const { return m_segments; }
  180. auto& split_lines() const
  181. {
  182. if (!m_split_lines.has_value()) {
  183. const_cast<Path*>(this)->segmentize_path();
  184. VERIFY(m_split_lines.has_value());
  185. }
  186. return m_split_lines.value();
  187. }
  188. void clear()
  189. {
  190. m_segments.clear();
  191. m_split_lines.clear();
  192. }
  193. Gfx::FloatRect const& bounding_box() const
  194. {
  195. if (!m_bounding_box.has_value()) {
  196. const_cast<Path*>(this)->segmentize_path();
  197. VERIFY(m_bounding_box.has_value());
  198. }
  199. return m_bounding_box.value();
  200. }
  201. void append_path(Path const& path)
  202. {
  203. m_segments.ensure_capacity(m_segments.size() + path.m_segments.size());
  204. for (auto const& segment : path.m_segments)
  205. m_segments.unchecked_append(segment);
  206. invalidate_split_lines();
  207. }
  208. Path copy_transformed(AffineTransform const&) const;
  209. void add_path(Path const&);
  210. DeprecatedString to_deprecated_string() const;
  211. private:
  212. void invalidate_split_lines()
  213. {
  214. m_split_lines.clear();
  215. }
  216. void segmentize_path();
  217. template<typename T, typename... Args>
  218. void append_segment(Args&&... args)
  219. {
  220. m_segments.append(adopt_ref(*new T(forward<Args>(args)...)));
  221. }
  222. Vector<NonnullRefPtr<Segment const>> m_segments {};
  223. Optional<Vector<SplitLineSegment>> m_split_lines {};
  224. Optional<Gfx::FloatRect> m_bounding_box;
  225. };
  226. }