RecordingPainter.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2023-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/Command.h>
  34. #include <LibWeb/Painting/CommandList.h>
  35. #include <LibWeb/Painting/GradientData.h>
  36. #include <LibWeb/Painting/PaintOuterBoxShadowParams.h>
  37. namespace Web::Painting {
  38. class RecordingPainter {
  39. AK_MAKE_NONCOPYABLE(RecordingPainter);
  40. AK_MAKE_NONMOVABLE(RecordingPainter);
  41. public:
  42. void fill_rect(Gfx::IntRect const& rect, Color color);
  43. struct FillPathUsingColorParams {
  44. Gfx::Path path;
  45. Gfx::Color color;
  46. Gfx::Painter::WindingRule winding_rule = Gfx::Painter::WindingRule::EvenOdd;
  47. Optional<Gfx::FloatPoint> translation = {};
  48. };
  49. void fill_path(FillPathUsingColorParams params);
  50. struct FillPathUsingPaintStyleParams {
  51. Gfx::Path path;
  52. NonnullRefPtr<Gfx::PaintStyle> paint_style;
  53. Gfx::Painter::WindingRule winding_rule = Gfx::Painter::WindingRule::EvenOdd;
  54. float opacity;
  55. Optional<Gfx::FloatPoint> translation = {};
  56. };
  57. void fill_path(FillPathUsingPaintStyleParams params);
  58. struct StrokePathUsingColorParams {
  59. Gfx::Path path;
  60. Gfx::Color color;
  61. float thickness;
  62. Optional<Gfx::FloatPoint> translation = {};
  63. };
  64. void stroke_path(StrokePathUsingColorParams params);
  65. struct StrokePathUsingPaintStyleParams {
  66. Gfx::Path path;
  67. NonnullRefPtr<Gfx::PaintStyle> paint_style;
  68. float thickness;
  69. float opacity;
  70. Optional<Gfx::FloatPoint> translation = {};
  71. };
  72. void stroke_path(StrokePathUsingPaintStyleParams params);
  73. void draw_ellipse(Gfx::IntRect const& a_rect, Color color, int thickness);
  74. void fill_ellipse(Gfx::IntRect const& a_rect, Color color, Gfx::AntiAliasingPainter::BlendMode blend_mode = Gfx::AntiAliasingPainter::BlendMode::Normal);
  75. void fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data);
  76. void fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position);
  77. void fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size);
  78. void draw_rect(Gfx::IntRect const& rect, Color color, bool rough = false);
  79. void draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor);
  80. void draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor);
  81. void draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness = 1, Gfx::Painter::LineStyle style = Gfx::Painter::LineStyle::Solid, Color alternate_color = Color::Transparent);
  82. void draw_text(Gfx::IntRect const&, String, Gfx::Font const&, Gfx::TextAlignment = Gfx::TextAlignment::TopLeft, Color = Color::Black, Gfx::TextElision = Gfx::TextElision::None, Gfx::TextWrapping = Gfx::TextWrapping::DontWrap);
  83. void draw_signed_distance_field(Gfx::IntRect const& dst_rect, Color color, Gfx::GrayscaleBitmap const& sdf, float smoothing);
  84. // Streamlined text drawing routine that does no wrapping/elision/alignment.
  85. void draw_text_run(Gfx::IntPoint baseline_start, Span<Gfx::DrawGlyphOrEmoji const> glyph_run, Color color, Gfx::IntRect const& rect);
  86. void add_clip_rect(Gfx::IntRect const& rect);
  87. void translate(int dx, int dy);
  88. void translate(Gfx::IntPoint delta);
  89. void set_scroll_frame_id(i32 id)
  90. {
  91. state().scroll_frame_id = id;
  92. }
  93. void save();
  94. void restore();
  95. struct PushStackingContextParams {
  96. float opacity;
  97. bool is_fixed_position;
  98. Gfx::IntRect source_paintable_rect;
  99. CSS::ImageRendering image_rendering;
  100. StackingContextTransform transform;
  101. Optional<StackingContextMask> mask = {};
  102. };
  103. void push_stacking_context(PushStackingContextParams params);
  104. void pop_stacking_context();
  105. void sample_under_corners(u32 id, CornerRadii corner_radii, Gfx::IntRect border_rect, CornerClip corner_clip);
  106. void blit_corner_clipping(u32 id, Gfx::IntRect border_rect);
  107. void paint_frame(Gfx::IntRect rect, Palette palette, Gfx::FrameStyle style);
  108. void apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter);
  109. void paint_outer_box_shadow_params(PaintOuterBoxShadowParams params);
  110. void paint_inner_box_shadow_params(PaintOuterBoxShadowParams params);
  111. void paint_text_shadow(int blur_radius, Gfx::IntRect bounding_rect, Gfx::IntRect text_rect, Span<Gfx::DrawGlyphOrEmoji const> glyph_run, Color color, int fragment_baseline, Gfx::IntPoint draw_location);
  112. void fill_rect_with_rounded_corners(Gfx::IntRect const& rect, Color color, Gfx::AntiAliasingPainter::CornerRadius top_left_radius, Gfx::AntiAliasingPainter::CornerRadius top_right_radius, Gfx::AntiAliasingPainter::CornerRadius bottom_right_radius, Gfx::AntiAliasingPainter::CornerRadius bottom_left_radius);
  113. void fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius);
  114. void fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius);
  115. void draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness);
  116. void paint_borders(DevicePixelRect const& border_rect, CornerRadii const& corner_radii, BordersDataDevicePixels const& borders_data);
  117. RecordingPainter(CommandList& commands_list);
  118. CommandList& commands_list() { return m_command_list; }
  119. void append(Command&& command);
  120. private:
  121. struct State {
  122. Gfx::AffineTransform translation;
  123. Optional<Gfx::IntRect> clip_rect;
  124. Optional<i32> scroll_frame_id;
  125. };
  126. State& state() { return m_state_stack.last(); }
  127. State const& state() const { return m_state_stack.last(); }
  128. Vector<State> m_state_stack;
  129. CommandList& m_command_list;
  130. };
  131. class RecordingPainterStateSaver {
  132. public:
  133. explicit RecordingPainterStateSaver(RecordingPainter& painter)
  134. : m_painter(painter)
  135. {
  136. m_painter.save();
  137. }
  138. ~RecordingPainterStateSaver()
  139. {
  140. m_painter.restore();
  141. }
  142. private:
  143. RecordingPainter& m_painter;
  144. };
  145. }