PaintStyle.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/QuickSort.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/Vector.h>
  13. #include <LibGfx/Bitmap.h>
  14. #include <LibGfx/Color.h>
  15. #include <LibGfx/Forward.h>
  16. #include <LibGfx/Gradients.h>
  17. #include <LibGfx/Rect.h>
  18. namespace Gfx {
  19. class PaintStyle : public RefCounted<PaintStyle> {
  20. public:
  21. virtual ~PaintStyle() = default;
  22. using SamplerFunction = Function<Color(IntPoint)>;
  23. using PaintFunction = Function<void(SamplerFunction)>;
  24. // Paint styles that have paint time dependent state (e.g. based on the paint size) may find it easier to override paint().
  25. // If paint() is overridden sample_color() is unused.
  26. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const
  27. {
  28. (void)physical_bounding_box;
  29. paint([this](IntPoint point) { return sample_color(point); });
  30. }
  31. private:
  32. // Simple paint styles can simply override sample_color() if they can easily generate a color from a coordinate.
  33. virtual Color sample_color(IntPoint) const { return Color(); }
  34. };
  35. class SolidColorPaintStyle final : public PaintStyle {
  36. public:
  37. static ErrorOr<NonnullRefPtr<SolidColorPaintStyle>> create(Color color)
  38. {
  39. return adopt_nonnull_ref_or_enomem(new (nothrow) SolidColorPaintStyle(color));
  40. }
  41. virtual Color sample_color(IntPoint) const override { return m_color; }
  42. private:
  43. SolidColorPaintStyle(Color color)
  44. : m_color(color)
  45. {
  46. }
  47. Color m_color;
  48. };
  49. class BitmapPaintStyle : public PaintStyle {
  50. public:
  51. static ErrorOr<NonnullRefPtr<BitmapPaintStyle>> create(Bitmap const& bitmap, IntPoint offset = {})
  52. {
  53. return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapPaintStyle(bitmap, offset));
  54. }
  55. virtual Color sample_color(IntPoint point) const override
  56. {
  57. point += m_offset;
  58. if (m_bitmap->rect().contains(point))
  59. return m_bitmap->get_pixel(point);
  60. return Color();
  61. }
  62. private:
  63. BitmapPaintStyle(Bitmap const& bitmap, IntPoint offset)
  64. : m_bitmap(bitmap)
  65. , m_offset(offset)
  66. {
  67. }
  68. NonnullRefPtr<Bitmap const> m_bitmap;
  69. IntPoint m_offset;
  70. };
  71. class RepeatingBitmapPaintStyle : public Gfx::PaintStyle {
  72. public:
  73. static ErrorOr<NonnullRefPtr<RepeatingBitmapPaintStyle>> create(Gfx::Bitmap const& bitmap, Gfx::IntPoint steps, Color fallback)
  74. {
  75. return adopt_nonnull_ref_or_enomem(new (nothrow) RepeatingBitmapPaintStyle(bitmap, steps, fallback));
  76. }
  77. virtual Color sample_color(Gfx::IntPoint point) const override
  78. {
  79. point.set_x(point.x() % m_steps.x());
  80. point.set_y(point.y() % m_steps.y());
  81. if (point.x() < 0 || point.y() < 0 || point.x() >= m_bitmap->width() || point.y() >= m_bitmap->height())
  82. return m_fallback;
  83. auto px = m_bitmap->get_pixel(point);
  84. return px;
  85. }
  86. private:
  87. RepeatingBitmapPaintStyle(Gfx::Bitmap const& bitmap, Gfx::IntPoint steps, Color fallback)
  88. : m_bitmap(bitmap)
  89. , m_steps(steps)
  90. , m_fallback(fallback)
  91. {
  92. }
  93. NonnullRefPtr<Gfx::Bitmap const> m_bitmap;
  94. Gfx::IntPoint m_steps;
  95. Color m_fallback;
  96. };
  97. class OffsetPaintStyle : public Gfx::PaintStyle {
  98. public:
  99. static ErrorOr<NonnullRefPtr<OffsetPaintStyle>> create(RefPtr<PaintStyle> other, Gfx::AffineTransform transform)
  100. {
  101. return adopt_nonnull_ref_or_enomem(new (nothrow) OffsetPaintStyle(move(other), transform));
  102. }
  103. virtual void paint(Gfx::IntRect physical_bounding_box, PaintFunction paint) const override
  104. {
  105. m_other->paint(m_transform.map(physical_bounding_box), [=, this, paint = move(paint)](SamplerFunction sampler) {
  106. paint([=, this, sampler = move(sampler)](Gfx::IntPoint point) {
  107. return sampler(m_transform.map(point));
  108. });
  109. });
  110. }
  111. private:
  112. OffsetPaintStyle(RefPtr<PaintStyle> other, Gfx::AffineTransform transform)
  113. : m_other(move(other))
  114. , m_transform(transform)
  115. {
  116. }
  117. RefPtr<PaintStyle> m_other;
  118. Gfx::AffineTransform m_transform;
  119. };
  120. class GradientPaintStyle : public PaintStyle {
  121. public:
  122. ErrorOr<void> add_color_stop(float position, Color color, Optional<float> transition_hint = {})
  123. {
  124. return add_color_stop(ColorStop { color, position, transition_hint });
  125. }
  126. ErrorOr<void> add_color_stop(ColorStop stop, bool sort = true)
  127. {
  128. TRY(m_color_stops.try_append(stop));
  129. if (sort)
  130. quick_sort(m_color_stops, [](auto& a, auto& b) { return a.position < b.position; });
  131. return {};
  132. }
  133. void set_repeat_length(float repeat_length)
  134. {
  135. m_repeat_length = repeat_length;
  136. }
  137. ReadonlySpan<ColorStop> color_stops() const { return m_color_stops; }
  138. Optional<float> repeat_length() const { return m_repeat_length; }
  139. private:
  140. Vector<ColorStop, 4> m_color_stops;
  141. Optional<float> m_repeat_length;
  142. };
  143. // These paint styles are based on the CSS gradients. They are relative to the painted
  144. // shape and support premultiplied alpha.
  145. class LinearGradientPaintStyle final : public GradientPaintStyle {
  146. public:
  147. static ErrorOr<ErrorOr<NonnullRefPtr<LinearGradientPaintStyle>>> create(float angle = 0.0f)
  148. {
  149. return adopt_nonnull_ref_or_enomem(new (nothrow) LinearGradientPaintStyle(angle));
  150. }
  151. private:
  152. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  153. LinearGradientPaintStyle(float angle)
  154. : m_angle(angle)
  155. {
  156. }
  157. float m_angle { 0.0f };
  158. };
  159. class ConicGradientPaintStyle final : public GradientPaintStyle {
  160. public:
  161. static ErrorOr<NonnullRefPtr<ConicGradientPaintStyle>> create(IntPoint center, float start_angle = 0.0f)
  162. {
  163. return adopt_nonnull_ref_or_enomem(new (nothrow) ConicGradientPaintStyle(center, start_angle));
  164. }
  165. private:
  166. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  167. ConicGradientPaintStyle(IntPoint center, float start_angle)
  168. : m_center(center)
  169. , m_start_angle(start_angle)
  170. {
  171. }
  172. IntPoint m_center;
  173. float m_start_angle { 0.0f };
  174. };
  175. class RadialGradientPaintStyle final : public GradientPaintStyle {
  176. public:
  177. static ErrorOr<NonnullRefPtr<RadialGradientPaintStyle>> create(IntPoint center, IntSize size)
  178. {
  179. return adopt_nonnull_ref_or_enomem(new (nothrow) RadialGradientPaintStyle(center, size));
  180. }
  181. private:
  182. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  183. RadialGradientPaintStyle(IntPoint center, IntSize size)
  184. : m_center(center)
  185. , m_size(size)
  186. {
  187. }
  188. IntPoint m_center;
  189. IntSize m_size;
  190. };
  191. // The following paint styles implement the gradients required for the HTML canvas.
  192. // These gradients are (unlike CSS ones) not relative to the painted shape, and do not
  193. // support premultiplied alpha.
  194. class CanvasLinearGradientPaintStyle final : public GradientPaintStyle {
  195. public:
  196. static ErrorOr<NonnullRefPtr<CanvasLinearGradientPaintStyle>> create(FloatPoint p0, FloatPoint p1)
  197. {
  198. return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasLinearGradientPaintStyle(p0, p1));
  199. }
  200. private:
  201. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  202. CanvasLinearGradientPaintStyle(FloatPoint p0, FloatPoint p1)
  203. : m_p0(p0)
  204. , m_p1(p1)
  205. {
  206. }
  207. FloatPoint m_p0;
  208. FloatPoint m_p1;
  209. };
  210. class CanvasConicGradientPaintStyle final : public GradientPaintStyle {
  211. public:
  212. static ErrorOr<NonnullRefPtr<CanvasConicGradientPaintStyle>> create(FloatPoint center, float start_angle = 0.0f)
  213. {
  214. return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasConicGradientPaintStyle(center, start_angle));
  215. }
  216. private:
  217. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  218. CanvasConicGradientPaintStyle(FloatPoint center, float start_angle)
  219. : m_center(center)
  220. , m_start_angle(start_angle)
  221. {
  222. }
  223. FloatPoint m_center;
  224. float m_start_angle { 0.0f };
  225. };
  226. class CanvasRadialGradientPaintStyle final : public GradientPaintStyle {
  227. public:
  228. static ErrorOr<NonnullRefPtr<CanvasRadialGradientPaintStyle>> create(FloatPoint start_center, float start_radius, FloatPoint end_center, float end_radius)
  229. {
  230. return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasRadialGradientPaintStyle(start_center, start_radius, end_center, end_radius));
  231. }
  232. private:
  233. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  234. CanvasRadialGradientPaintStyle(FloatPoint start_center, float start_radius, FloatPoint end_center, float end_radius)
  235. : m_start_center(start_center)
  236. , m_start_radius(start_radius)
  237. , m_end_center(end_center)
  238. , m_end_radius(end_radius)
  239. {
  240. }
  241. FloatPoint m_start_center;
  242. float m_start_radius { 0.0f };
  243. FloatPoint m_end_center;
  244. float m_end_radius { 0.0f };
  245. };
  246. // The following paint styles implement the gradients required for SVGs
  247. class SVGGradientPaintStyle : public GradientPaintStyle {
  248. public:
  249. void set_gradient_transform(Gfx::AffineTransform transform);
  250. enum class SpreadMethod {
  251. Pad,
  252. Repeat,
  253. Reflect
  254. };
  255. void set_spread_method(SpreadMethod spread_method)
  256. {
  257. m_spread_method = spread_method;
  258. }
  259. protected:
  260. Optional<AffineTransform> const& scale_adjusted_inverse_gradient_transform() const { return m_inverse_transform; }
  261. float gradient_transform_scale() const { return m_scale; }
  262. SpreadMethod spread_method() const { return m_spread_method; }
  263. private:
  264. Optional<AffineTransform> m_inverse_transform {};
  265. float m_scale = 1.0f;
  266. SpreadMethod m_spread_method { SpreadMethod::Pad };
  267. };
  268. class SVGLinearGradientPaintStyle final : public SVGGradientPaintStyle {
  269. public:
  270. static ErrorOr<NonnullRefPtr<SVGLinearGradientPaintStyle>> create(FloatPoint p0, FloatPoint p1)
  271. {
  272. return adopt_nonnull_ref_or_enomem(new (nothrow) SVGLinearGradientPaintStyle(p0, p1));
  273. }
  274. void set_start_point(FloatPoint start_point)
  275. {
  276. m_p0 = start_point;
  277. }
  278. void set_end_point(FloatPoint end_point)
  279. {
  280. m_p1 = end_point;
  281. }
  282. private:
  283. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  284. SVGLinearGradientPaintStyle(FloatPoint p0, FloatPoint p1)
  285. : m_p0(p0)
  286. , m_p1(p1)
  287. {
  288. }
  289. FloatPoint m_p0;
  290. FloatPoint m_p1;
  291. };
  292. class SVGRadialGradientPaintStyle final : public SVGGradientPaintStyle {
  293. public:
  294. static ErrorOr<NonnullRefPtr<SVGRadialGradientPaintStyle>> create(FloatPoint start_center, float start_radius, FloatPoint end_center, float end_radius)
  295. {
  296. return adopt_nonnull_ref_or_enomem(new (nothrow) SVGRadialGradientPaintStyle(start_center, start_radius, end_center, end_radius));
  297. }
  298. void set_start_center(FloatPoint start_center)
  299. {
  300. m_start_center = start_center;
  301. }
  302. void set_start_radius(float start_radius)
  303. {
  304. m_start_radius = start_radius;
  305. }
  306. void set_end_center(FloatPoint end_center)
  307. {
  308. m_end_center = end_center;
  309. }
  310. void set_end_radius(float end_radius)
  311. {
  312. m_end_radius = end_radius;
  313. }
  314. private:
  315. virtual void paint(IntRect physical_bounding_box, PaintFunction paint) const override;
  316. SVGRadialGradientPaintStyle(FloatPoint start_center, float start_radius, FloatPoint end_center, float end_radius)
  317. : m_start_center(start_center)
  318. , m_start_radius(start_radius)
  319. , m_end_center(end_center)
  320. , m_end_radius(end_radius)
  321. {
  322. }
  323. FloatPoint m_start_center;
  324. float m_start_radius { 0.0f };
  325. FloatPoint m_end_center;
  326. float m_end_radius { 0.0f };
  327. };
  328. }