Command.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. Vector<Gfx::DrawGlyphOrEmoji> glyph_run;
  38. Color color;
  39. Gfx::IntRect rect;
  40. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  41. void translate_by(Gfx::IntPoint const& offset);
  42. };
  43. struct DrawText {
  44. Gfx::IntRect rect;
  45. String raw_text;
  46. Gfx::TextAlignment alignment;
  47. Color color;
  48. Gfx::TextElision elision;
  49. Gfx::TextWrapping wrapping;
  50. Optional<NonnullRefPtr<Gfx::Font>> font {};
  51. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  52. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  53. };
  54. struct FillRect {
  55. Gfx::IntRect rect;
  56. Color color;
  57. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  58. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  59. };
  60. struct DrawScaledBitmap {
  61. Gfx::IntRect dst_rect;
  62. NonnullRefPtr<Gfx::Bitmap> bitmap;
  63. Gfx::IntRect src_rect;
  64. Gfx::Painter::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 DrawScaledImmutableBitmap {
  69. Gfx::IntRect dst_rect;
  70. NonnullRefPtr<Gfx::ImmutableBitmap> bitmap;
  71. Gfx::IntRect src_rect;
  72. Gfx::Painter::ScalingMode scaling_mode;
  73. [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
  74. void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); }
  75. };
  76. struct SetClipRect {
  77. Gfx::IntRect rect;
  78. };
  79. struct ClearClipRect { };
  80. struct StackingContextTransform {
  81. Gfx::FloatPoint origin;
  82. Gfx::FloatMatrix4x4 matrix;
  83. };
  84. struct StackingContextMask {
  85. NonnullRefPtr<Gfx::Bitmap> mask_bitmap;
  86. Gfx::Bitmap::MaskKind mask_kind;
  87. };
  88. struct PushStackingContext {
  89. float opacity;
  90. bool is_fixed_position;
  91. // The bounding box of the source paintable (pre-transform).
  92. Gfx::IntRect source_paintable_rect;
  93. // A translation to be applied after the stacking context has been transformed.
  94. Gfx::IntPoint post_transform_translation;
  95. CSS::ImageRendering image_rendering;
  96. StackingContextTransform transform;
  97. Optional<StackingContextMask> mask = {};
  98. void translate_by(Gfx::IntPoint const& offset)
  99. {
  100. source_paintable_rect.translate_by(offset);
  101. }
  102. };
  103. struct PopStackingContext { };
  104. struct PaintLinearGradient {
  105. Gfx::IntRect gradient_rect;
  106. LinearGradientData linear_gradient_data;
  107. [[nodiscard]] Gfx::IntRect bounding_rect() const { return gradient_rect; }
  108. void translate_by(Gfx::IntPoint const& offset)
  109. {
  110. gradient_rect.translate_by(offset);
  111. }
  112. };
  113. struct PaintOuterBoxShadow {
  114. PaintOuterBoxShadowParams outer_box_shadow_params;
  115. [[nodiscard]] Gfx::IntRect bounding_rect() const;
  116. void translate_by(Gfx::IntPoint const& offset);
  117. };
  118. struct PaintInnerBoxShadow {
  119. PaintOuterBoxShadowParams outer_box_shadow_params;
  120. void translate_by(Gfx::IntPoint const& offset);
  121. };
  122. struct PaintTextShadow {
  123. int blur_radius;
  124. Gfx::IntRect shadow_bounding_rect;
  125. Gfx::IntRect text_rect;
  126. Vector<Gfx::DrawGlyphOrEmoji> glyph_run;
  127. Color color;
  128. int fragment_baseline;
  129. Gfx::IntPoint draw_location;
  130. [[nodiscard]] Gfx::IntRect bounding_rect() const { return { draw_location, shadow_bounding_rect.size() }; }
  131. void translate_by(Gfx::IntPoint const& offset) { draw_location.translate_by(offset); }
  132. };
  133. struct FillRectWithRoundedCorners {
  134. Gfx::IntRect rect;
  135. Color color;
  136. Gfx::AntiAliasingPainter::CornerRadius top_left_radius;
  137. Gfx::AntiAliasingPainter::CornerRadius top_right_radius;
  138. Gfx::AntiAliasingPainter::CornerRadius bottom_left_radius;
  139. Gfx::AntiAliasingPainter::CornerRadius bottom_right_radius;
  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::Painter::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. NonnullRefPtr<Gfx::PaintStyle> paint_style;
  160. Gfx::Painter::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. NonnullRefPtr<Gfx::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. Gfx::AntiAliasingPainter::BlendMode blend_mode;
  211. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  212. void translate_by(Gfx::IntPoint const& offset)
  213. {
  214. rect.translate_by(offset);
  215. }
  216. };
  217. struct DrawLine {
  218. Color color;
  219. Gfx::IntPoint from;
  220. Gfx::IntPoint to;
  221. int thickness;
  222. Gfx::Painter::LineStyle style;
  223. Color alternate_color;
  224. void translate_by(Gfx::IntPoint const& offset)
  225. {
  226. from.translate_by(offset);
  227. to.translate_by(offset);
  228. }
  229. };
  230. struct DrawSignedDistanceField {
  231. Gfx::IntRect rect;
  232. Color color;
  233. Gfx::GrayscaleBitmap sdf;
  234. float smoothing;
  235. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  236. void translate_by(Gfx::IntPoint const& offset)
  237. {
  238. rect.translate_by(offset);
  239. }
  240. };
  241. struct PaintFrame {
  242. Gfx::IntRect rect;
  243. Palette palette;
  244. Gfx::FrameStyle style;
  245. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  246. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  247. };
  248. struct ApplyBackdropFilter {
  249. Gfx::IntRect backdrop_region;
  250. BorderRadiiData border_radii_data;
  251. CSS::ResolvedBackdropFilter backdrop_filter;
  252. [[nodiscard]] Gfx::IntRect bounding_rect() const { return backdrop_region; }
  253. void translate_by(Gfx::IntPoint const& offset)
  254. {
  255. backdrop_region.translate_by(offset);
  256. }
  257. };
  258. struct DrawRect {
  259. Gfx::IntRect rect;
  260. Color color;
  261. bool rough;
  262. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  263. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  264. };
  265. struct PaintRadialGradient {
  266. Gfx::IntRect rect;
  267. RadialGradientData radial_gradient_data;
  268. Gfx::IntPoint center;
  269. Gfx::IntSize size;
  270. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  271. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  272. };
  273. struct PaintConicGradient {
  274. Gfx::IntRect rect;
  275. ConicGradientData conic_gradient_data;
  276. Gfx::IntPoint position;
  277. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  278. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  279. };
  280. struct DrawTriangleWave {
  281. Gfx::IntPoint p1;
  282. Gfx::IntPoint p2;
  283. Color color;
  284. int amplitude;
  285. int thickness;
  286. void translate_by(Gfx::IntPoint const& offset)
  287. {
  288. p1.translate_by(offset);
  289. p2.translate_by(offset);
  290. }
  291. };
  292. struct SampleUnderCorners {
  293. u32 id;
  294. CornerRadii corner_radii;
  295. Gfx::IntRect border_rect;
  296. CornerClip corner_clip;
  297. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  298. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  299. };
  300. struct BlitCornerClipping {
  301. u32 id;
  302. Gfx::IntRect border_rect;
  303. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  304. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  305. };
  306. struct PaintBorders {
  307. DevicePixelRect border_rect;
  308. CornerRadii corner_radii;
  309. BordersDataDevicePixels borders_data;
  310. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect.to_type<int>(); }
  311. void translate_by(Gfx::IntPoint const& offset)
  312. {
  313. border_rect.translate_by(offset.to_type<DevicePixels>());
  314. }
  315. };
  316. using Command = Variant<
  317. DrawGlyphRun,
  318. DrawText,
  319. FillRect,
  320. DrawScaledBitmap,
  321. DrawScaledImmutableBitmap,
  322. SetClipRect,
  323. ClearClipRect,
  324. PushStackingContext,
  325. PopStackingContext,
  326. PaintLinearGradient,
  327. PaintRadialGradient,
  328. PaintConicGradient,
  329. PaintOuterBoxShadow,
  330. PaintInnerBoxShadow,
  331. PaintTextShadow,
  332. FillRectWithRoundedCorners,
  333. FillPathUsingColor,
  334. FillPathUsingPaintStyle,
  335. StrokePathUsingColor,
  336. StrokePathUsingPaintStyle,
  337. DrawEllipse,
  338. FillEllipse,
  339. DrawLine,
  340. DrawSignedDistanceField,
  341. PaintFrame,
  342. ApplyBackdropFilter,
  343. DrawRect,
  344. DrawTriangleWave,
  345. SampleUnderCorners,
  346. BlitCornerClipping,
  347. PaintBorders>;
  348. }