Command.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/SegmentedVector.h>
  10. #include <AK/Utf8View.h>
  11. #include <AK/Vector.h>
  12. #include <LibGfx/AntiAliasingPainter.h>
  13. #include <LibGfx/Color.h>
  14. #include <LibGfx/Forward.h>
  15. #include <LibGfx/Gradients.h>
  16. #include <LibGfx/ImmutableBitmap.h>
  17. #include <LibGfx/PaintStyle.h>
  18. #include <LibGfx/Palette.h>
  19. #include <LibGfx/Point.h>
  20. #include <LibGfx/Rect.h>
  21. #include <LibGfx/ScalingMode.h>
  22. #include <LibGfx/Size.h>
  23. #include <LibGfx/TextAlignment.h>
  24. #include <LibGfx/TextLayout.h>
  25. #include <LibWeb/CSS/Enums.h>
  26. #include <LibWeb/Painting/BorderRadiiData.h>
  27. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  28. #include <LibWeb/Painting/GradientData.h>
  29. #include <LibWeb/Painting/PaintBoxShadowParams.h>
  30. #include <LibWeb/Painting/PaintStyle.h>
  31. namespace Web::Painting {
  32. class DisplayList;
  33. struct DrawGlyphRun {
  34. NonnullRefPtr<Gfx::GlyphRun> glyph_run;
  35. Color color;
  36. Gfx::IntRect rect;
  37. Gfx::FloatPoint translation;
  38. double scale { 1 };
  39. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  40. void translate_by(Gfx::IntPoint const& offset);
  41. };
  42. struct FillRect {
  43. Gfx::IntRect rect;
  44. Color color;
  45. RefPtr<DisplayList> text_clip;
  46. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  47. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  48. };
  49. struct DrawScaledBitmap {
  50. Gfx::IntRect dst_rect;
  51. NonnullRefPtr<Gfx::Bitmap> bitmap;
  52. Gfx::IntRect src_rect;
  53. Gfx::ScalingMode scaling_mode;
  54. [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
  55. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  56. };
  57. struct DrawScaledImmutableBitmap {
  58. Gfx::IntRect dst_rect;
  59. NonnullRefPtr<Gfx::ImmutableBitmap> bitmap;
  60. Gfx::IntRect src_rect;
  61. Gfx::ScalingMode scaling_mode;
  62. RefPtr<DisplayList> text_clip;
  63. [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
  64. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  65. };
  66. struct DrawRepeatedImmutableBitmap {
  67. struct Repeat {
  68. bool x { false };
  69. bool y { false };
  70. };
  71. Gfx::IntRect dst_rect;
  72. Gfx::IntRect clip_rect;
  73. NonnullRefPtr<Gfx::ImmutableBitmap> bitmap;
  74. Gfx::ScalingMode scaling_mode;
  75. Repeat repeat;
  76. RefPtr<DisplayList> text_clip;
  77. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  78. };
  79. struct Save { };
  80. struct Restore { };
  81. struct AddClipRect {
  82. Gfx::IntRect rect;
  83. };
  84. struct StackingContextTransform {
  85. Gfx::FloatPoint origin;
  86. Gfx::FloatMatrix4x4 matrix;
  87. };
  88. struct StackingContextMask {
  89. NonnullRefPtr<Gfx::Bitmap> mask_bitmap;
  90. Gfx::Bitmap::MaskKind mask_kind;
  91. };
  92. struct PushStackingContext {
  93. float opacity;
  94. bool is_fixed_position;
  95. // The bounding box of the source paintable (pre-transform).
  96. Gfx::IntRect source_paintable_rect;
  97. // A translation to be applied after the stacking context has been transformed.
  98. Gfx::IntPoint post_transform_translation;
  99. CSS::ImageRendering image_rendering;
  100. StackingContextTransform transform;
  101. Optional<StackingContextMask> mask = {};
  102. void translate_by(Gfx::IntPoint const& offset)
  103. {
  104. source_paintable_rect.translate_by(offset);
  105. }
  106. };
  107. struct PopStackingContext { };
  108. struct PaintLinearGradient {
  109. Gfx::IntRect gradient_rect;
  110. LinearGradientData linear_gradient_data;
  111. RefPtr<DisplayList> text_clip;
  112. [[nodiscard]] Gfx::IntRect bounding_rect() const { return gradient_rect; }
  113. void translate_by(Gfx::IntPoint const& offset)
  114. {
  115. gradient_rect.translate_by(offset);
  116. }
  117. };
  118. struct PaintOuterBoxShadow {
  119. PaintBoxShadowParams box_shadow_params;
  120. [[nodiscard]] Gfx::IntRect bounding_rect() const;
  121. void translate_by(Gfx::IntPoint const& offset);
  122. };
  123. struct PaintInnerBoxShadow {
  124. PaintBoxShadowParams box_shadow_params;
  125. [[nodiscard]] Gfx::IntRect bounding_rect() const;
  126. void translate_by(Gfx::IntPoint const& offset);
  127. };
  128. struct PaintTextShadow {
  129. int blur_radius;
  130. Gfx::IntRect shadow_bounding_rect;
  131. Gfx::IntRect text_rect;
  132. NonnullRefPtr<Gfx::GlyphRun> glyph_run;
  133. double glyph_run_scale { 1 };
  134. Color color;
  135. Gfx::IntPoint draw_location;
  136. [[nodiscard]] Gfx::IntRect bounding_rect() const { return { draw_location, shadow_bounding_rect.size() }; }
  137. void translate_by(Gfx::IntPoint const& offset) { draw_location.translate_by(offset); }
  138. };
  139. struct FillRectWithRoundedCorners {
  140. Gfx::IntRect rect;
  141. Color color;
  142. CornerRadii corner_radii;
  143. RefPtr<DisplayList> text_clip;
  144. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  145. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  146. };
  147. struct FillPathUsingColor {
  148. Gfx::IntRect path_bounding_rect;
  149. Gfx::Path path;
  150. Color color;
  151. Gfx::WindingRule winding_rule;
  152. Gfx::FloatPoint aa_translation;
  153. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  154. void translate_by(Gfx::IntPoint const& offset)
  155. {
  156. path_bounding_rect.translate_by(offset);
  157. aa_translation.translate_by(offset.to_type<float>());
  158. }
  159. };
  160. struct FillPathUsingPaintStyle {
  161. Gfx::IntRect path_bounding_rect;
  162. Gfx::Path path;
  163. PaintStyle paint_style;
  164. Gfx::WindingRule winding_rule;
  165. float opacity;
  166. Gfx::FloatPoint aa_translation;
  167. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  168. void translate_by(Gfx::IntPoint const& offset)
  169. {
  170. path_bounding_rect.translate_by(offset);
  171. aa_translation.translate_by(offset.to_type<float>());
  172. }
  173. };
  174. struct StrokePathUsingColor {
  175. Gfx::IntRect path_bounding_rect;
  176. Gfx::Path path;
  177. Color color;
  178. float thickness;
  179. Gfx::FloatPoint aa_translation;
  180. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  181. void translate_by(Gfx::IntPoint const& offset)
  182. {
  183. path_bounding_rect.translate_by(offset);
  184. aa_translation.translate_by(offset.to_type<float>());
  185. }
  186. };
  187. struct StrokePathUsingPaintStyle {
  188. Gfx::IntRect path_bounding_rect;
  189. Gfx::Path path;
  190. PaintStyle paint_style;
  191. float thickness;
  192. float opacity = 1.0f;
  193. Gfx::FloatPoint aa_translation;
  194. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  195. void translate_by(Gfx::IntPoint const& offset)
  196. {
  197. path_bounding_rect.translate_by(offset);
  198. aa_translation.translate_by(offset.to_type<float>());
  199. }
  200. };
  201. struct DrawEllipse {
  202. Gfx::IntRect rect;
  203. Color color;
  204. int thickness;
  205. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  206. void translate_by(Gfx::IntPoint const& offset)
  207. {
  208. rect.translate_by(offset);
  209. }
  210. };
  211. struct FillEllipse {
  212. Gfx::IntRect rect;
  213. Color color;
  214. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  215. void translate_by(Gfx::IntPoint const& offset)
  216. {
  217. rect.translate_by(offset);
  218. }
  219. };
  220. struct DrawLine {
  221. Color color;
  222. Gfx::IntPoint from;
  223. Gfx::IntPoint to;
  224. int thickness;
  225. Gfx::LineStyle style;
  226. Color alternate_color;
  227. void translate_by(Gfx::IntPoint const& offset)
  228. {
  229. from.translate_by(offset);
  230. to.translate_by(offset);
  231. }
  232. };
  233. struct ApplyBackdropFilter {
  234. Gfx::IntRect backdrop_region;
  235. BorderRadiiData border_radii_data;
  236. CSS::ResolvedBackdropFilter backdrop_filter;
  237. [[nodiscard]] Gfx::IntRect bounding_rect() const { return backdrop_region; }
  238. void translate_by(Gfx::IntPoint const& offset)
  239. {
  240. backdrop_region.translate_by(offset);
  241. }
  242. };
  243. struct DrawRect {
  244. Gfx::IntRect rect;
  245. Color color;
  246. bool rough;
  247. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  248. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  249. };
  250. struct PaintRadialGradient {
  251. Gfx::IntRect rect;
  252. RadialGradientData radial_gradient_data;
  253. Gfx::IntPoint center;
  254. Gfx::IntSize size;
  255. RefPtr<DisplayList> text_clip;
  256. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  257. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  258. };
  259. struct PaintConicGradient {
  260. Gfx::IntRect rect;
  261. ConicGradientData conic_gradient_data;
  262. Gfx::IntPoint position;
  263. RefPtr<DisplayList> text_clip;
  264. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  265. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  266. };
  267. struct DrawTriangleWave {
  268. Gfx::IntPoint p1;
  269. Gfx::IntPoint p2;
  270. Color color;
  271. int amplitude;
  272. int thickness;
  273. void translate_by(Gfx::IntPoint const& offset)
  274. {
  275. p1.translate_by(offset);
  276. p2.translate_by(offset);
  277. }
  278. };
  279. struct SampleUnderCorners {
  280. u32 id;
  281. CornerRadii corner_radii;
  282. Gfx::IntRect border_rect;
  283. CornerClip corner_clip;
  284. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  285. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  286. };
  287. struct BlitCornerClipping {
  288. u32 id;
  289. Gfx::IntRect border_rect;
  290. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  291. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  292. };
  293. using Command = Variant<
  294. DrawGlyphRun,
  295. FillRect,
  296. DrawScaledBitmap,
  297. DrawScaledImmutableBitmap,
  298. DrawRepeatedImmutableBitmap,
  299. Save,
  300. Restore,
  301. AddClipRect,
  302. PushStackingContext,
  303. PopStackingContext,
  304. PaintLinearGradient,
  305. PaintRadialGradient,
  306. PaintConicGradient,
  307. PaintOuterBoxShadow,
  308. PaintInnerBoxShadow,
  309. PaintTextShadow,
  310. FillRectWithRoundedCorners,
  311. FillPathUsingColor,
  312. FillPathUsingPaintStyle,
  313. StrokePathUsingColor,
  314. StrokePathUsingPaintStyle,
  315. DrawEllipse,
  316. FillEllipse,
  317. DrawLine,
  318. ApplyBackdropFilter,
  319. DrawRect,
  320. DrawTriangleWave,
  321. SampleUnderCorners,
  322. BlitCornerClipping>;
  323. }