Painter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/Memory.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/Utf8View.h>
  11. #include <AK/Vector.h>
  12. #include <LibGfx/Color.h>
  13. #include <LibGfx/Font/FontDatabase.h>
  14. #include <LibGfx/Forward.h>
  15. #include <LibGfx/Gradients.h>
  16. #include <LibGfx/GrayscaleBitmap.h>
  17. #include <LibGfx/PaintStyle.h>
  18. #include <LibGfx/Point.h>
  19. #include <LibGfx/Rect.h>
  20. #include <LibGfx/Size.h>
  21. #include <LibGfx/TextAlignment.h>
  22. #include <LibGfx/TextDirection.h>
  23. #include <LibGfx/TextElision.h>
  24. #include <LibGfx/TextWrapping.h>
  25. namespace Gfx {
  26. class Painter {
  27. public:
  28. static constexpr int LINE_SPACING = 4;
  29. explicit Painter(Gfx::Bitmap&);
  30. ~Painter() = default;
  31. enum class LineStyle {
  32. Solid,
  33. Dotted,
  34. Dashed,
  35. };
  36. enum class ScalingMode {
  37. NearestFractional,
  38. NearestNeighbor,
  39. SmoothPixels,
  40. BilinearBlend,
  41. None,
  42. };
  43. void clear_rect(IntRect const&, Color);
  44. void fill_rect(IntRect const&, Color);
  45. void fill_rect(IntRect const&, PaintStyle const&);
  46. void fill_rect_with_dither_pattern(IntRect const&, Color, Color);
  47. void fill_rect_with_checkerboard(IntRect const&, IntSize, Color color_dark, Color color_light);
  48. void fill_rect_with_gradient(Orientation, IntRect const&, Color gradient_start, Color gradient_end);
  49. void fill_rect_with_gradient(IntRect const&, Color gradient_start, Color gradient_end);
  50. void fill_rect_with_linear_gradient(IntRect const&, ReadonlySpan<ColorStop>, float angle, Optional<float> repeat_length = {});
  51. void fill_rect_with_conic_gradient(IntRect const&, ReadonlySpan<ColorStop>, IntPoint center, float start_angle, Optional<float> repeat_length = {});
  52. void fill_rect_with_radial_gradient(IntRect const&, ReadonlySpan<ColorStop>, IntPoint center, IntSize size, Optional<float> repeat_length = {});
  53. void fill_rect_with_rounded_corners(IntRect const&, Color, int radius);
  54. void fill_rect_with_rounded_corners(IntRect const&, Color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius);
  55. void fill_ellipse(IntRect const&, Color);
  56. void draw_rect(IntRect const&, Color, bool rough = false);
  57. void draw_rect_with_thickness(IntRect const&, Color, int thickness);
  58. void draw_focus_rect(IntRect const&, Color);
  59. void draw_bitmap(IntPoint, CharacterBitmap const&, Color = Color());
  60. void draw_bitmap(IntPoint, GlyphBitmap const&, Color = Color());
  61. void draw_scaled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&, IntRect const& src_rect, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
  62. void draw_scaled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&, FloatRect const& src_rect, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
  63. void draw_scaled_bitmap_with_transform(IntRect const& dst_rect, Gfx::Bitmap const&, FloatRect const& src_rect, Gfx::AffineTransform const&, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
  64. void draw_triangle(IntPoint, IntPoint, IntPoint, Color);
  65. void draw_triangle(IntPoint offset, ReadonlySpan<IntPoint>, Color);
  66. void draw_ellipse_intersecting(IntRect const&, Color, int thickness = 1);
  67. void set_pixel(IntPoint, Color, bool blend = false);
  68. void set_pixel(int x, int y, Color color, bool blend = false) { set_pixel({ x, y }, color, blend); }
  69. Optional<Color> get_pixel(IntPoint);
  70. ErrorOr<NonnullRefPtr<Bitmap>> get_region_bitmap(IntRect const&, BitmapFormat format, Optional<IntRect&> actual_region = {});
  71. void draw_line(IntPoint, IntPoint, Color, int thickness = 1, LineStyle style = LineStyle::Solid, Color alternate_color = Color::Transparent);
  72. void draw_triangle_wave(IntPoint, IntPoint, Color color, int amplitude, int thickness = 1);
  73. void draw_quadratic_bezier_curve(IntPoint control_point, IntPoint, IntPoint, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
  74. void draw_cubic_bezier_curve(IntPoint control_point_0, IntPoint control_point_1, IntPoint, IntPoint, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
  75. void draw_elliptical_arc(IntPoint p1, IntPoint p2, IntPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
  76. void blit(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, float opacity = 1.0f, bool apply_alpha = true);
  77. void blit_dimmed(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect);
  78. void blit_brightened(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect);
  79. void blit_filtered(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, Function<Color(Color)>);
  80. void draw_tiled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&);
  81. void blit_offset(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, IntPoint);
  82. void blit_disabled(IntPoint, Gfx::Bitmap const&, IntRect const&, Palette const&);
  83. void blit_tiled(IntRect const&, Gfx::Bitmap const&, IntRect const& src_rect);
  84. void draw_text(FloatRect const&, StringView, Font const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  85. void draw_text(FloatRect const&, StringView, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  86. void draw_text(FloatRect const&, Utf32View const&, Font const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  87. void draw_text(FloatRect const&, Utf32View const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  88. void draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)>, FloatRect const&, StringView, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  89. void draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)>, FloatRect const&, Utf8View const&, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  90. void draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)>, FloatRect const&, Utf32View const&, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  91. void draw_text(IntRect const&, StringView, Font const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  92. void draw_text(IntRect const&, StringView, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  93. void draw_text(IntRect const&, Utf32View const&, Font const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  94. void draw_text(IntRect const&, Utf32View const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  95. void draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)>, IntRect const&, StringView, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  96. void draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)>, IntRect const&, Utf8View const&, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  97. void draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)>, IntRect const&, Utf32View const&, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
  98. void draw_ui_text(Gfx::IntRect const&, StringView, Gfx::Font const&, TextAlignment, Gfx::Color);
  99. void draw_glyph(IntPoint, u32, Color);
  100. void draw_glyph(IntPoint, u32, Font const&, Color);
  101. void draw_emoji(IntPoint, Gfx::Bitmap const&, Font const&);
  102. void draw_glyph_or_emoji(IntPoint, u32, Font const&, Color);
  103. void draw_glyph_or_emoji(IntPoint, Utf8CodePointIterator&, Font const&, Color);
  104. void draw_glyph(FloatPoint, u32, Color);
  105. void draw_glyph(FloatPoint, u32, Font const&, Color);
  106. void draw_glyph_or_emoji(FloatPoint, u32, Font const&, Color);
  107. void draw_glyph_or_emoji(FloatPoint, Utf8CodePointIterator&, Font const&, Color);
  108. void draw_circle_arc_intersecting(IntRect const&, IntPoint, int radius, Color, int thickness);
  109. void draw_signed_distance_field(IntRect const& dst_rect, Color, Gfx::GrayscaleBitmap const&, float smoothing);
  110. // Streamlined text drawing routine that does no wrapping/elision/alignment.
  111. void draw_text_run(IntPoint baseline_start, Utf8View const&, Font const&, Color);
  112. void draw_text_run(FloatPoint baseline_start, Utf8View const&, Font const&, Color);
  113. enum class CornerOrientation {
  114. TopLeft,
  115. TopRight,
  116. BottomRight,
  117. BottomLeft
  118. };
  119. void fill_rounded_corner(IntRect const&, int radius, Color, CornerOrientation);
  120. static void for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&);
  121. static void for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&&);
  122. static void for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&);
  123. static void for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&&);
  124. static void for_each_line_segment_on_elliptical_arc(FloatPoint p1, FloatPoint p2, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint, FloatPoint)>&);
  125. static void for_each_line_segment_on_elliptical_arc(FloatPoint p1, FloatPoint p2, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint, FloatPoint)>&&);
  126. void stroke_path(Path const&, Color, int thickness);
  127. enum class WindingRule {
  128. Nonzero,
  129. EvenOdd,
  130. };
  131. void fill_path(Path const&, Color, WindingRule rule = WindingRule::Nonzero);
  132. void fill_path(Path const&, PaintStyle const& paint_style, WindingRule rule = WindingRule::Nonzero);
  133. Font const& font() const
  134. {
  135. if (!state().font)
  136. return FontDatabase::default_font();
  137. return *state().font;
  138. }
  139. void set_font(Font const& font) { state().font = &font; }
  140. enum class DrawOp {
  141. Copy,
  142. Xor,
  143. Invert
  144. };
  145. void set_draw_op(DrawOp op) { state().draw_op = op; }
  146. DrawOp draw_op() const { return state().draw_op; }
  147. void add_clip_rect(IntRect const& rect);
  148. void clear_clip_rect();
  149. void translate(int dx, int dy) { translate({ dx, dy }); }
  150. void translate(IntPoint delta) { state().translation.translate_by(delta); }
  151. IntPoint translation() const { return state().translation; }
  152. Gfx::Bitmap* target() { return m_target.ptr(); }
  153. void save() { m_state_stack.append(m_state_stack.last()); }
  154. void restore()
  155. {
  156. VERIFY(m_state_stack.size() > 1);
  157. m_state_stack.take_last();
  158. }
  159. IntRect clip_rect() const { return state().clip_rect; }
  160. int scale() const { return state().scale; }
  161. protected:
  162. friend GradientLine;
  163. friend AntiAliasingPainter;
  164. IntRect to_physical(IntRect const& r) const { return r.translated(translation()) * scale(); }
  165. IntPoint to_physical(IntPoint p) const { return p.translated(translation()) * scale(); }
  166. void set_physical_pixel_with_draw_op(u32& pixel, Color);
  167. void fill_physical_scanline_with_draw_op(int y, int x, int width, Color color);
  168. void fill_rect_with_draw_op(IntRect const&, Color);
  169. void blit_with_opacity(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, float opacity, bool apply_alpha = true);
  170. void draw_physical_pixel(IntPoint, Color, int thickness = 1);
  171. void set_physical_pixel(IntPoint, Color color, bool blend);
  172. struct State {
  173. Font const* font;
  174. IntPoint translation;
  175. int scale = 1;
  176. IntRect clip_rect;
  177. DrawOp draw_op;
  178. };
  179. State& state() { return m_state_stack.last(); }
  180. State const& state() const { return m_state_stack.last(); }
  181. void fill_physical_rect(IntRect const&, Color);
  182. IntRect m_clip_origin;
  183. NonnullRefPtr<Gfx::Bitmap> m_target;
  184. Vector<State, 4> m_state_stack;
  185. private:
  186. Vector<DirectionalRun> split_text_into_directional_runs(Utf8View const&, TextDirection initial_direction);
  187. bool text_contains_bidirectional_text(Utf8View const&, TextDirection);
  188. template<typename DrawGlyphFunction>
  189. void do_draw_text(FloatRect const&, Utf8View const& text, Font const&, TextAlignment, TextElision, TextWrapping, DrawGlyphFunction);
  190. void antialiased_fill_path(Path const&, Color, WindingRule rule, FloatPoint translation);
  191. void antialiased_fill_path(Path const&, PaintStyle const& paint_style, WindingRule rule, FloatPoint translation);
  192. enum class FillPathMode {
  193. PlaceOnIntGrid,
  194. AllowFloatingPoints,
  195. };
  196. template<typename T, typename TColorOrFunction>
  197. void draw_scanline_for_fill_path(int y, T x_start, T x_end, TColorOrFunction color);
  198. template<FillPathMode fill_path_mode, typename ColorOrFunction>
  199. void fill_path_impl(Path const& path, ColorOrFunction color, Gfx::Painter::WindingRule winding_rule, Optional<FloatPoint> offset = {});
  200. };
  201. class PainterStateSaver {
  202. public:
  203. explicit PainterStateSaver(Painter&);
  204. ~PainterStateSaver();
  205. private:
  206. Painter& m_painter;
  207. };
  208. DeprecatedString parse_ampersand_string(StringView, Optional<size_t>* underline_offset = nullptr);
  209. }