Command.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  217. void translate_by(Gfx::IntPoint const& offset)
  218. {
  219. rect.translate_by(offset);
  220. }
  221. };
  222. struct DrawLine {
  223. Color color;
  224. Gfx::IntPoint from;
  225. Gfx::IntPoint to;
  226. int thickness;
  227. Gfx::Painter::LineStyle style;
  228. Color alternate_color;
  229. void translate_by(Gfx::IntPoint const& offset)
  230. {
  231. from.translate_by(offset);
  232. to.translate_by(offset);
  233. }
  234. };
  235. struct DrawSignedDistanceField {
  236. Gfx::IntRect rect;
  237. Color color;
  238. Gfx::GrayscaleBitmap sdf;
  239. float smoothing;
  240. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  241. void translate_by(Gfx::IntPoint const& offset)
  242. {
  243. rect.translate_by(offset);
  244. }
  245. };
  246. struct ApplyBackdropFilter {
  247. Gfx::IntRect backdrop_region;
  248. BorderRadiiData border_radii_data;
  249. CSS::ResolvedBackdropFilter backdrop_filter;
  250. [[nodiscard]] Gfx::IntRect bounding_rect() const { return backdrop_region; }
  251. void translate_by(Gfx::IntPoint const& offset)
  252. {
  253. backdrop_region.translate_by(offset);
  254. }
  255. };
  256. struct DrawRect {
  257. Gfx::IntRect rect;
  258. Color color;
  259. bool rough;
  260. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  261. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  262. };
  263. struct PaintRadialGradient {
  264. Gfx::IntRect rect;
  265. RadialGradientData radial_gradient_data;
  266. Gfx::IntPoint center;
  267. Gfx::IntSize size;
  268. Vector<Gfx::Path> clip_paths;
  269. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  270. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  271. };
  272. struct PaintConicGradient {
  273. Gfx::IntRect rect;
  274. ConicGradientData conic_gradient_data;
  275. Gfx::IntPoint position;
  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 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. using Command = Variant<
  307. DrawGlyphRun,
  308. DrawText,
  309. FillRect,
  310. DrawScaledBitmap,
  311. DrawScaledImmutableBitmap,
  312. SetClipRect,
  313. ClearClipRect,
  314. PushStackingContext,
  315. PopStackingContext,
  316. PaintLinearGradient,
  317. PaintRadialGradient,
  318. PaintConicGradient,
  319. PaintOuterBoxShadow,
  320. PaintInnerBoxShadow,
  321. PaintTextShadow,
  322. FillRectWithRoundedCorners,
  323. FillPathUsingColor,
  324. FillPathUsingPaintStyle,
  325. StrokePathUsingColor,
  326. StrokePathUsingPaintStyle,
  327. DrawEllipse,
  328. FillEllipse,
  329. DrawLine,
  330. DrawSignedDistanceField,
  331. ApplyBackdropFilter,
  332. DrawRect,
  333. DrawTriangleWave,
  334. SampleUnderCorners,
  335. BlitCornerClipping>;
  336. }