Command.h 12 KB

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