Command.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/Path.h>
  20. #include <LibGfx/Point.h>
  21. #include <LibGfx/Rect.h>
  22. #include <LibGfx/ScalingMode.h>
  23. #include <LibGfx/Size.h>
  24. #include <LibGfx/TextAlignment.h>
  25. #include <LibGfx/TextLayout.h>
  26. #include <LibWeb/CSS/Enums.h>
  27. #include <LibWeb/Painting/BorderRadiiData.h>
  28. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  29. #include <LibWeb/Painting/GradientData.h>
  30. #include <LibWeb/Painting/PaintBoxShadowParams.h>
  31. #include <LibWeb/Painting/PaintStyle.h>
  32. namespace Web::Painting {
  33. class DisplayList;
  34. struct DrawGlyphRun {
  35. NonnullRefPtr<Gfx::GlyphRun> glyph_run;
  36. Color color;
  37. Gfx::IntRect rect;
  38. Gfx::FloatPoint translation;
  39. double scale { 1 };
  40. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  41. void translate_by(Gfx::IntPoint const& offset);
  42. };
  43. struct FillRect {
  44. Gfx::IntRect rect;
  45. Color color;
  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. [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
  63. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  64. };
  65. struct DrawRepeatedImmutableBitmap {
  66. struct Repeat {
  67. bool x { false };
  68. bool y { false };
  69. };
  70. Gfx::IntRect dst_rect;
  71. Gfx::IntRect clip_rect;
  72. NonnullRefPtr<Gfx::ImmutableBitmap> bitmap;
  73. Gfx::ScalingMode scaling_mode;
  74. Repeat repeat;
  75. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  76. };
  77. struct Save { };
  78. struct Restore { };
  79. struct Translate {
  80. Gfx::IntPoint delta;
  81. void translate_by(Gfx::IntPoint const& offset) { delta.translate_by(offset); }
  82. };
  83. struct AddClipRect {
  84. Gfx::IntRect rect;
  85. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  86. };
  87. struct StackingContextTransform {
  88. Gfx::FloatPoint origin;
  89. Gfx::FloatMatrix4x4 matrix;
  90. };
  91. struct PushStackingContext {
  92. float opacity;
  93. CSS::ResolvedFilter filter;
  94. // The bounding box of the source paintable (pre-transform).
  95. Gfx::IntRect source_paintable_rect;
  96. // A translation to be applied after the stacking context has been transformed.
  97. StackingContextTransform transform;
  98. Optional<Gfx::Path> clip_path = {};
  99. void translate_by(Gfx::IntPoint const& offset)
  100. {
  101. source_paintable_rect.translate_by(offset);
  102. transform.origin.translate_by(offset.to_type<float>());
  103. }
  104. };
  105. struct PopStackingContext { };
  106. struct PaintLinearGradient {
  107. Gfx::IntRect gradient_rect;
  108. LinearGradientData linear_gradient_data;
  109. [[nodiscard]] Gfx::IntRect bounding_rect() const { return gradient_rect; }
  110. void translate_by(Gfx::IntPoint const& offset)
  111. {
  112. gradient_rect.translate_by(offset);
  113. }
  114. };
  115. struct PaintOuterBoxShadow {
  116. PaintBoxShadowParams box_shadow_params;
  117. [[nodiscard]] Gfx::IntRect bounding_rect() const;
  118. void translate_by(Gfx::IntPoint const& offset);
  119. };
  120. struct PaintInnerBoxShadow {
  121. PaintBoxShadowParams box_shadow_params;
  122. [[nodiscard]] Gfx::IntRect bounding_rect() const;
  123. void translate_by(Gfx::IntPoint const& offset);
  124. };
  125. struct PaintTextShadow {
  126. int blur_radius;
  127. Gfx::IntRect shadow_bounding_rect;
  128. Gfx::IntRect text_rect;
  129. NonnullRefPtr<Gfx::GlyphRun> glyph_run;
  130. double glyph_run_scale { 1 };
  131. Color color;
  132. Gfx::IntPoint draw_location;
  133. [[nodiscard]] Gfx::IntRect bounding_rect() const { return { draw_location, shadow_bounding_rect.size() }; }
  134. void translate_by(Gfx::IntPoint const& offset) { draw_location.translate_by(offset); }
  135. };
  136. struct FillRectWithRoundedCorners {
  137. Gfx::IntRect rect;
  138. Color color;
  139. CornerRadii corner_radii;
  140. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  141. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  142. };
  143. struct FillPathUsingColor {
  144. Gfx::IntRect path_bounding_rect;
  145. Gfx::Path path;
  146. Color color;
  147. Gfx::WindingRule winding_rule;
  148. Gfx::FloatPoint aa_translation;
  149. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  150. void translate_by(Gfx::IntPoint const& offset)
  151. {
  152. path_bounding_rect.translate_by(offset);
  153. aa_translation.translate_by(offset.to_type<float>());
  154. }
  155. };
  156. struct FillPathUsingPaintStyle {
  157. Gfx::IntRect path_bounding_rect;
  158. Gfx::Path path;
  159. PaintStyle paint_style;
  160. Gfx::WindingRule winding_rule;
  161. float opacity;
  162. Gfx::FloatPoint aa_translation;
  163. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  164. void translate_by(Gfx::IntPoint const& offset)
  165. {
  166. path_bounding_rect.translate_by(offset);
  167. aa_translation.translate_by(offset.to_type<float>());
  168. }
  169. };
  170. struct StrokePathUsingColor {
  171. Gfx::IntRect path_bounding_rect;
  172. Gfx::Path path;
  173. Color color;
  174. float thickness;
  175. Gfx::FloatPoint aa_translation;
  176. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  177. void translate_by(Gfx::IntPoint const& offset)
  178. {
  179. path_bounding_rect.translate_by(offset);
  180. aa_translation.translate_by(offset.to_type<float>());
  181. }
  182. };
  183. struct StrokePathUsingPaintStyle {
  184. Gfx::IntRect path_bounding_rect;
  185. Gfx::Path path;
  186. PaintStyle paint_style;
  187. float thickness;
  188. float opacity = 1.0f;
  189. Gfx::FloatPoint aa_translation;
  190. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  191. void translate_by(Gfx::IntPoint const& offset)
  192. {
  193. path_bounding_rect.translate_by(offset);
  194. aa_translation.translate_by(offset.to_type<float>());
  195. }
  196. };
  197. struct DrawEllipse {
  198. Gfx::IntRect rect;
  199. Color color;
  200. int thickness;
  201. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  202. void translate_by(Gfx::IntPoint const& offset)
  203. {
  204. rect.translate_by(offset);
  205. }
  206. };
  207. struct FillEllipse {
  208. Gfx::IntRect rect;
  209. Color color;
  210. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  211. void translate_by(Gfx::IntPoint const& offset)
  212. {
  213. rect.translate_by(offset);
  214. }
  215. };
  216. struct DrawLine {
  217. Color color;
  218. Gfx::IntPoint from;
  219. Gfx::IntPoint to;
  220. int thickness;
  221. Gfx::LineStyle style;
  222. Color alternate_color;
  223. void translate_by(Gfx::IntPoint const& offset)
  224. {
  225. from.translate_by(offset);
  226. to.translate_by(offset);
  227. }
  228. };
  229. struct ApplyBackdropFilter {
  230. Gfx::IntRect backdrop_region;
  231. BorderRadiiData border_radii_data;
  232. CSS::ResolvedFilter backdrop_filter;
  233. [[nodiscard]] Gfx::IntRect bounding_rect() const { return backdrop_region; }
  234. void translate_by(Gfx::IntPoint const& offset)
  235. {
  236. backdrop_region.translate_by(offset);
  237. }
  238. };
  239. struct DrawRect {
  240. Gfx::IntRect rect;
  241. Color color;
  242. bool rough;
  243. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  244. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  245. };
  246. struct PaintRadialGradient {
  247. Gfx::IntRect rect;
  248. RadialGradientData radial_gradient_data;
  249. Gfx::IntPoint center;
  250. Gfx::IntSize size;
  251. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  252. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  253. };
  254. struct PaintConicGradient {
  255. Gfx::IntRect rect;
  256. ConicGradientData conic_gradient_data;
  257. Gfx::IntPoint position;
  258. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  259. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  260. };
  261. struct DrawTriangleWave {
  262. Gfx::IntPoint p1;
  263. Gfx::IntPoint p2;
  264. Color color;
  265. int amplitude;
  266. int thickness;
  267. void translate_by(Gfx::IntPoint const& offset)
  268. {
  269. p1.translate_by(offset);
  270. p2.translate_by(offset);
  271. }
  272. };
  273. struct AddRoundedRectClip {
  274. CornerRadii corner_radii;
  275. Gfx::IntRect border_rect;
  276. CornerClip corner_clip;
  277. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  278. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  279. };
  280. struct AddMask {
  281. RefPtr<DisplayList> display_list;
  282. Gfx::IntRect rect;
  283. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  284. void translate_by(Gfx::IntPoint const& offset)
  285. {
  286. rect.translate_by(offset);
  287. }
  288. };
  289. struct PaintNestedDisplayList {
  290. RefPtr<DisplayList> display_list;
  291. Gfx::IntRect rect;
  292. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  293. void translate_by(Gfx::IntPoint const& offset)
  294. {
  295. rect.translate_by(offset);
  296. }
  297. };
  298. struct PaintScrollBar {
  299. int scroll_frame_id;
  300. Gfx::IntRect rect;
  301. CSSPixelFraction scroll_size;
  302. bool vertical;
  303. void translate_by(Gfx::IntPoint const& offset)
  304. {
  305. rect.translate_by(offset);
  306. }
  307. };
  308. struct ApplyOpacity {
  309. float opacity;
  310. };
  311. struct ApplyTransform {
  312. Gfx::FloatPoint origin;
  313. Gfx::FloatMatrix4x4 matrix;
  314. void translate_by(Gfx::IntPoint const& offset)
  315. {
  316. origin.translate_by(offset.to_type<float>());
  317. }
  318. };
  319. struct ApplyMaskBitmap {
  320. Gfx::IntPoint origin;
  321. NonnullRefPtr<Gfx::Bitmap> bitmap;
  322. Gfx::Bitmap::MaskKind kind;
  323. void translate_by(Gfx::IntPoint const& offset)
  324. {
  325. origin.translate_by(offset);
  326. }
  327. };
  328. using Command = Variant<
  329. DrawGlyphRun,
  330. FillRect,
  331. DrawScaledBitmap,
  332. DrawScaledImmutableBitmap,
  333. DrawRepeatedImmutableBitmap,
  334. Save,
  335. Restore,
  336. Translate,
  337. AddClipRect,
  338. PushStackingContext,
  339. PopStackingContext,
  340. PaintLinearGradient,
  341. PaintRadialGradient,
  342. PaintConicGradient,
  343. PaintOuterBoxShadow,
  344. PaintInnerBoxShadow,
  345. PaintTextShadow,
  346. FillRectWithRoundedCorners,
  347. FillPathUsingColor,
  348. FillPathUsingPaintStyle,
  349. StrokePathUsingColor,
  350. StrokePathUsingPaintStyle,
  351. DrawEllipse,
  352. FillEllipse,
  353. DrawLine,
  354. ApplyBackdropFilter,
  355. DrawRect,
  356. DrawTriangleWave,
  357. AddRoundedRectClip,
  358. AddMask,
  359. PaintNestedDisplayList,
  360. PaintScrollBar,
  361. ApplyOpacity,
  362. ApplyTransform,
  363. ApplyMaskBitmap>;
  364. }