Command.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/GrayscaleBitmap.h>
  17. #include <LibGfx/ImmutableBitmap.h>
  18. #include <LibGfx/PaintStyle.h>
  19. #include <LibGfx/Painter.h>
  20. #include <LibGfx/Palette.h>
  21. #include <LibGfx/Point.h>
  22. #include <LibGfx/Rect.h>
  23. #include <LibGfx/Size.h>
  24. #include <LibGfx/StylePainter.h>
  25. #include <LibGfx/TextAlignment.h>
  26. #include <LibGfx/TextDirection.h>
  27. #include <LibGfx/TextElision.h>
  28. #include <LibGfx/TextLayout.h>
  29. #include <LibGfx/TextWrapping.h>
  30. #include <LibWeb/CSS/Enums.h>
  31. #include <LibWeb/Painting/BorderRadiiData.h>
  32. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  33. #include <LibWeb/Painting/GradientData.h>
  34. #include <LibWeb/Painting/PaintOuterBoxShadowParams.h>
  35. namespace Web::Painting {
  36. struct DrawGlyphRun {
  37. NonnullRefPtr<Gfx::GlyphRun> glyph_run;
  38. Color color;
  39. Gfx::IntRect rect;
  40. Gfx::FloatPoint translation;
  41. double scale { 1 };
  42. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  43. void translate_by(Gfx::IntPoint const& offset);
  44. };
  45. struct DrawText {
  46. Gfx::IntRect rect;
  47. String raw_text;
  48. Gfx::TextAlignment alignment;
  49. Color color;
  50. Gfx::TextElision elision;
  51. Gfx::TextWrapping wrapping;
  52. Optional<NonnullRefPtr<Gfx::Font>> font {};
  53. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  54. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  55. };
  56. struct FillRect {
  57. Gfx::IntRect rect;
  58. Color color;
  59. Vector<Gfx::Path> clip_paths;
  60. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  61. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  62. };
  63. struct DrawScaledBitmap {
  64. Gfx::IntRect dst_rect;
  65. NonnullRefPtr<Gfx::Bitmap> bitmap;
  66. Gfx::IntRect src_rect;
  67. Gfx::Painter::ScalingMode scaling_mode;
  68. [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
  69. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  70. };
  71. struct DrawScaledImmutableBitmap {
  72. Gfx::IntRect dst_rect;
  73. NonnullRefPtr<Gfx::ImmutableBitmap> bitmap;
  74. Gfx::IntRect src_rect;
  75. Gfx::Painter::ScalingMode scaling_mode;
  76. Vector<Gfx::Path> clip_paths;
  77. [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
  78. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  79. };
  80. struct SetClipRect {
  81. Gfx::IntRect rect;
  82. };
  83. struct ClearClipRect { };
  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. Vector<Gfx::Path> clip_paths;
  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. PaintOuterBoxShadowParams outer_box_shadow_params;
  120. [[nodiscard]] Gfx::IntRect bounding_rect() const;
  121. void translate_by(Gfx::IntPoint const& offset);
  122. };
  123. struct PaintInnerBoxShadow {
  124. PaintOuterBoxShadowParams outer_box_shadow_params;
  125. void translate_by(Gfx::IntPoint const& offset);
  126. };
  127. struct PaintTextShadow {
  128. int blur_radius;
  129. Gfx::IntRect shadow_bounding_rect;
  130. Gfx::IntRect text_rect;
  131. Vector<Gfx::DrawGlyphOrEmoji> glyph_run;
  132. Color color;
  133. int fragment_baseline;
  134. Gfx::IntPoint draw_location;
  135. [[nodiscard]] Gfx::IntRect bounding_rect() const { return { draw_location, shadow_bounding_rect.size() }; }
  136. void translate_by(Gfx::IntPoint const& offset) { draw_location.translate_by(offset); }
  137. };
  138. struct FillRectWithRoundedCorners {
  139. Gfx::IntRect rect;
  140. Color color;
  141. Gfx::AntiAliasingPainter::CornerRadius top_left_radius;
  142. Gfx::AntiAliasingPainter::CornerRadius top_right_radius;
  143. Gfx::AntiAliasingPainter::CornerRadius bottom_left_radius;
  144. Gfx::AntiAliasingPainter::CornerRadius bottom_right_radius;
  145. Vector<Gfx::Path> clip_paths;
  146. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  147. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  148. };
  149. struct FillPathUsingColor {
  150. Gfx::IntRect path_bounding_rect;
  151. Gfx::Path path;
  152. Color color;
  153. Gfx::Painter::WindingRule winding_rule;
  154. Gfx::FloatPoint aa_translation;
  155. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  156. void translate_by(Gfx::IntPoint const& offset)
  157. {
  158. path_bounding_rect.translate_by(offset);
  159. aa_translation.translate_by(offset.to_type<float>());
  160. }
  161. };
  162. struct FillPathUsingPaintStyle {
  163. Gfx::IntRect path_bounding_rect;
  164. Gfx::Path path;
  165. NonnullRefPtr<Gfx::PaintStyle> paint_style;
  166. Gfx::Painter::WindingRule winding_rule;
  167. float opacity;
  168. Gfx::FloatPoint aa_translation;
  169. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  170. void translate_by(Gfx::IntPoint const& offset)
  171. {
  172. path_bounding_rect.translate_by(offset);
  173. aa_translation.translate_by(offset.to_type<float>());
  174. }
  175. };
  176. struct StrokePathUsingColor {
  177. Gfx::IntRect path_bounding_rect;
  178. Gfx::Path path;
  179. Color color;
  180. float thickness;
  181. Gfx::FloatPoint aa_translation;
  182. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  183. void translate_by(Gfx::IntPoint const& offset)
  184. {
  185. path_bounding_rect.translate_by(offset);
  186. aa_translation.translate_by(offset.to_type<float>());
  187. }
  188. };
  189. struct StrokePathUsingPaintStyle {
  190. Gfx::IntRect path_bounding_rect;
  191. Gfx::Path path;
  192. NonnullRefPtr<Gfx::PaintStyle> paint_style;
  193. float thickness;
  194. float opacity = 1.0f;
  195. Gfx::FloatPoint aa_translation;
  196. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  197. void translate_by(Gfx::IntPoint const& offset)
  198. {
  199. path_bounding_rect.translate_by(offset);
  200. aa_translation.translate_by(offset.to_type<float>());
  201. }
  202. };
  203. struct DrawEllipse {
  204. Gfx::IntRect rect;
  205. Color color;
  206. int thickness;
  207. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  208. void translate_by(Gfx::IntPoint const& offset)
  209. {
  210. rect.translate_by(offset);
  211. }
  212. };
  213. struct FillEllipse {
  214. Gfx::IntRect rect;
  215. Color color;
  216. Gfx::AntiAliasingPainter::BlendMode blend_mode;
  217. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  218. void translate_by(Gfx::IntPoint const& offset)
  219. {
  220. rect.translate_by(offset);
  221. }
  222. };
  223. struct DrawLine {
  224. Color color;
  225. Gfx::IntPoint from;
  226. Gfx::IntPoint to;
  227. int thickness;
  228. Gfx::Painter::LineStyle style;
  229. Color alternate_color;
  230. void translate_by(Gfx::IntPoint const& offset)
  231. {
  232. from.translate_by(offset);
  233. to.translate_by(offset);
  234. }
  235. };
  236. struct DrawSignedDistanceField {
  237. Gfx::IntRect rect;
  238. Color color;
  239. Gfx::GrayscaleBitmap sdf;
  240. float smoothing;
  241. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  242. void translate_by(Gfx::IntPoint const& offset)
  243. {
  244. rect.translate_by(offset);
  245. }
  246. };
  247. struct PaintFrame {
  248. Gfx::IntRect rect;
  249. Palette palette;
  250. Gfx::FrameStyle style;
  251. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  252. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  253. };
  254. struct ApplyBackdropFilter {
  255. Gfx::IntRect backdrop_region;
  256. BorderRadiiData border_radii_data;
  257. CSS::ResolvedBackdropFilter backdrop_filter;
  258. [[nodiscard]] Gfx::IntRect bounding_rect() const { return backdrop_region; }
  259. void translate_by(Gfx::IntPoint const& offset)
  260. {
  261. backdrop_region.translate_by(offset);
  262. }
  263. };
  264. struct DrawRect {
  265. Gfx::IntRect rect;
  266. Color color;
  267. bool rough;
  268. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  269. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  270. };
  271. struct PaintRadialGradient {
  272. Gfx::IntRect rect;
  273. RadialGradientData radial_gradient_data;
  274. Gfx::IntPoint center;
  275. Gfx::IntSize size;
  276. Vector<Gfx::Path> clip_paths;
  277. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  278. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  279. };
  280. struct PaintConicGradient {
  281. Gfx::IntRect rect;
  282. ConicGradientData conic_gradient_data;
  283. Gfx::IntPoint position;
  284. Vector<Gfx::Path> clip_paths;
  285. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  286. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  287. };
  288. struct DrawTriangleWave {
  289. Gfx::IntPoint p1;
  290. Gfx::IntPoint p2;
  291. Color color;
  292. int amplitude;
  293. int thickness;
  294. void translate_by(Gfx::IntPoint const& offset)
  295. {
  296. p1.translate_by(offset);
  297. p2.translate_by(offset);
  298. }
  299. };
  300. struct SampleUnderCorners {
  301. u32 id;
  302. CornerRadii corner_radii;
  303. Gfx::IntRect border_rect;
  304. CornerClip corner_clip;
  305. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  306. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  307. };
  308. struct BlitCornerClipping {
  309. u32 id;
  310. Gfx::IntRect border_rect;
  311. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  312. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  313. };
  314. struct PaintBorders {
  315. DevicePixelRect border_rect;
  316. CornerRadii corner_radii;
  317. BordersDataDevicePixels borders_data;
  318. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect.to_type<int>(); }
  319. void translate_by(Gfx::IntPoint const& offset)
  320. {
  321. border_rect.translate_by(offset.to_type<DevicePixels>());
  322. }
  323. };
  324. using Command = Variant<
  325. DrawGlyphRun,
  326. DrawText,
  327. FillRect,
  328. DrawScaledBitmap,
  329. DrawScaledImmutableBitmap,
  330. SetClipRect,
  331. ClearClipRect,
  332. PushStackingContext,
  333. PopStackingContext,
  334. PaintLinearGradient,
  335. PaintRadialGradient,
  336. PaintConicGradient,
  337. PaintOuterBoxShadow,
  338. PaintInnerBoxShadow,
  339. PaintTextShadow,
  340. FillRectWithRoundedCorners,
  341. FillPathUsingColor,
  342. FillPathUsingPaintStyle,
  343. StrokePathUsingColor,
  344. StrokePathUsingPaintStyle,
  345. DrawEllipse,
  346. FillEllipse,
  347. DrawLine,
  348. DrawSignedDistanceField,
  349. PaintFrame,
  350. ApplyBackdropFilter,
  351. DrawRect,
  352. DrawTriangleWave,
  353. SampleUnderCorners,
  354. BlitCornerClipping,
  355. PaintBorders>;
  356. }