Command.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/Palette.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/TextDirection.h>
  26. #include <LibGfx/TextElision.h>
  27. #include <LibGfx/TextLayout.h>
  28. #include <LibGfx/TextWrapping.h>
  29. #include <LibWeb/CSS/Enums.h>
  30. #include <LibWeb/Painting/BorderRadiiData.h>
  31. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  32. #include <LibWeb/Painting/GradientData.h>
  33. #include <LibWeb/Painting/PaintBoxShadowParams.h>
  34. #include <LibWeb/Painting/PaintStyle.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. 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::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::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 Save { };
  81. struct Restore { };
  82. struct AddClipRect {
  83. Gfx::IntRect rect;
  84. };
  85. struct StackingContextTransform {
  86. Gfx::FloatPoint origin;
  87. Gfx::FloatMatrix4x4 matrix;
  88. };
  89. struct StackingContextMask {
  90. NonnullRefPtr<Gfx::Bitmap> mask_bitmap;
  91. Gfx::Bitmap::MaskKind mask_kind;
  92. };
  93. struct PushStackingContext {
  94. float opacity;
  95. bool is_fixed_position;
  96. // The bounding box of the source paintable (pre-transform).
  97. Gfx::IntRect source_paintable_rect;
  98. // A translation to be applied after the stacking context has been transformed.
  99. Gfx::IntPoint post_transform_translation;
  100. CSS::ImageRendering image_rendering;
  101. StackingContextTransform transform;
  102. Optional<StackingContextMask> mask = {};
  103. void translate_by(Gfx::IntPoint const& offset)
  104. {
  105. source_paintable_rect.translate_by(offset);
  106. }
  107. };
  108. struct PopStackingContext { };
  109. struct PaintLinearGradient {
  110. Gfx::IntRect gradient_rect;
  111. LinearGradientData linear_gradient_data;
  112. Vector<Gfx::Path> clip_paths;
  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. int blur_radius;
  131. Gfx::IntRect shadow_bounding_rect;
  132. Gfx::IntRect text_rect;
  133. Vector<Gfx::DrawGlyphOrEmoji> glyph_run;
  134. Color color;
  135. int fragment_baseline;
  136. Gfx::IntPoint draw_location;
  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. Gfx::AntiAliasingPainter::CornerRadius top_left_radius;
  144. Gfx::AntiAliasingPainter::CornerRadius top_right_radius;
  145. Gfx::AntiAliasingPainter::CornerRadius bottom_left_radius;
  146. Gfx::AntiAliasingPainter::CornerRadius bottom_right_radius;
  147. Vector<Gfx::Path> clip_paths;
  148. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  149. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  150. };
  151. struct FillPathUsingColor {
  152. Gfx::IntRect path_bounding_rect;
  153. Gfx::Path path;
  154. Color color;
  155. Gfx::WindingRule winding_rule;
  156. Gfx::FloatPoint aa_translation;
  157. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  158. void translate_by(Gfx::IntPoint const& offset)
  159. {
  160. path_bounding_rect.translate_by(offset);
  161. aa_translation.translate_by(offset.to_type<float>());
  162. }
  163. };
  164. struct FillPathUsingPaintStyle {
  165. Gfx::IntRect path_bounding_rect;
  166. Gfx::Path path;
  167. PaintStyle paint_style;
  168. Gfx::WindingRule winding_rule;
  169. float opacity;
  170. Gfx::FloatPoint aa_translation;
  171. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  172. void translate_by(Gfx::IntPoint const& offset)
  173. {
  174. path_bounding_rect.translate_by(offset);
  175. aa_translation.translate_by(offset.to_type<float>());
  176. }
  177. };
  178. struct StrokePathUsingColor {
  179. Gfx::IntRect path_bounding_rect;
  180. Gfx::Path path;
  181. Color color;
  182. float thickness;
  183. Gfx::FloatPoint aa_translation;
  184. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  185. void translate_by(Gfx::IntPoint const& offset)
  186. {
  187. path_bounding_rect.translate_by(offset);
  188. aa_translation.translate_by(offset.to_type<float>());
  189. }
  190. };
  191. struct StrokePathUsingPaintStyle {
  192. Gfx::IntRect path_bounding_rect;
  193. Gfx::Path path;
  194. PaintStyle paint_style;
  195. float thickness;
  196. float opacity = 1.0f;
  197. Gfx::FloatPoint aa_translation;
  198. [[nodiscard]] Gfx::IntRect bounding_rect() const { return path_bounding_rect; }
  199. void translate_by(Gfx::IntPoint const& offset)
  200. {
  201. path_bounding_rect.translate_by(offset);
  202. aa_translation.translate_by(offset.to_type<float>());
  203. }
  204. };
  205. struct DrawEllipse {
  206. Gfx::IntRect rect;
  207. Color color;
  208. int thickness;
  209. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  210. void translate_by(Gfx::IntPoint const& offset)
  211. {
  212. rect.translate_by(offset);
  213. }
  214. };
  215. struct FillEllipse {
  216. Gfx::IntRect rect;
  217. Color color;
  218. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  219. void translate_by(Gfx::IntPoint const& offset)
  220. {
  221. rect.translate_by(offset);
  222. }
  223. };
  224. struct DrawLine {
  225. Color color;
  226. Gfx::IntPoint from;
  227. Gfx::IntPoint to;
  228. int thickness;
  229. Gfx::LineStyle style;
  230. Color alternate_color;
  231. void translate_by(Gfx::IntPoint const& offset)
  232. {
  233. from.translate_by(offset);
  234. to.translate_by(offset);
  235. }
  236. };
  237. struct ApplyBackdropFilter {
  238. Gfx::IntRect backdrop_region;
  239. BorderRadiiData border_radii_data;
  240. CSS::ResolvedBackdropFilter backdrop_filter;
  241. [[nodiscard]] Gfx::IntRect bounding_rect() const { return backdrop_region; }
  242. void translate_by(Gfx::IntPoint const& offset)
  243. {
  244. backdrop_region.translate_by(offset);
  245. }
  246. };
  247. struct DrawRect {
  248. Gfx::IntRect rect;
  249. Color color;
  250. bool rough;
  251. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  252. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  253. };
  254. struct PaintRadialGradient {
  255. Gfx::IntRect rect;
  256. RadialGradientData radial_gradient_data;
  257. Gfx::IntPoint center;
  258. Gfx::IntSize size;
  259. Vector<Gfx::Path> clip_paths;
  260. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  261. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  262. };
  263. struct PaintConicGradient {
  264. Gfx::IntRect rect;
  265. ConicGradientData conic_gradient_data;
  266. Gfx::IntPoint position;
  267. Vector<Gfx::Path> clip_paths;
  268. [[nodiscard]] Gfx::IntRect bounding_rect() const { return rect; }
  269. void translate_by(Gfx::IntPoint const& offset) { rect.translate_by(offset); }
  270. };
  271. struct DrawTriangleWave {
  272. Gfx::IntPoint p1;
  273. Gfx::IntPoint p2;
  274. Color color;
  275. int amplitude;
  276. int thickness;
  277. void translate_by(Gfx::IntPoint const& offset)
  278. {
  279. p1.translate_by(offset);
  280. p2.translate_by(offset);
  281. }
  282. };
  283. struct SampleUnderCorners {
  284. u32 id;
  285. CornerRadii corner_radii;
  286. Gfx::IntRect border_rect;
  287. CornerClip corner_clip;
  288. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  289. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  290. };
  291. struct BlitCornerClipping {
  292. u32 id;
  293. Gfx::IntRect border_rect;
  294. [[nodiscard]] Gfx::IntRect bounding_rect() const { return border_rect; }
  295. void translate_by(Gfx::IntPoint const& offset) { border_rect.translate_by(offset); }
  296. };
  297. using Command = Variant<
  298. DrawGlyphRun,
  299. DrawText,
  300. FillRect,
  301. DrawScaledBitmap,
  302. DrawScaledImmutableBitmap,
  303. Save,
  304. Restore,
  305. AddClipRect,
  306. PushStackingContext,
  307. PopStackingContext,
  308. PaintLinearGradient,
  309. PaintRadialGradient,
  310. PaintConicGradient,
  311. PaintOuterBoxShadow,
  312. PaintInnerBoxShadow,
  313. PaintTextShadow,
  314. FillRectWithRoundedCorners,
  315. FillPathUsingColor,
  316. FillPathUsingPaintStyle,
  317. StrokePathUsingColor,
  318. StrokePathUsingPaintStyle,
  319. DrawEllipse,
  320. FillEllipse,
  321. DrawLine,
  322. ApplyBackdropFilter,
  323. DrawRect,
  324. DrawTriangleWave,
  325. SampleUnderCorners,
  326. BlitCornerClipping>;
  327. }