Painter.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/NonnullRefPtr.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/Color.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibGfx/Point.h>
  13. #include <LibGfx/Rect.h>
  14. #include <LibGfx/Size.h>
  15. #include <LibGfx/TextAlignment.h>
  16. #include <LibGfx/TextElision.h>
  17. namespace Gfx {
  18. class Painter {
  19. public:
  20. explicit Painter(Gfx::Bitmap&);
  21. ~Painter();
  22. enum class LineStyle {
  23. Solid,
  24. Dotted,
  25. Dashed,
  26. };
  27. void clear_rect(const IntRect&, Color);
  28. void fill_rect(const IntRect&, Color);
  29. void fill_rect_with_dither_pattern(const IntRect&, Color, Color);
  30. void fill_rect_with_checkerboard(const IntRect&, const IntSize&, Color color_dark, Color color_light);
  31. void fill_rect_with_gradient(Orientation, const IntRect&, Color gradient_start, Color gradient_end);
  32. void fill_rect_with_gradient(const IntRect&, Color gradient_start, Color gradient_end);
  33. void fill_rect_with_rounded_corners(const IntRect&, Color, int radius);
  34. void fill_rect_with_rounded_corners(const IntRect&, Color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius);
  35. void fill_ellipse(const IntRect&, Color);
  36. void draw_rect(const IntRect&, Color, bool rough = false);
  37. void draw_focus_rect(const IntRect&, Color);
  38. void draw_bitmap(const IntPoint&, const CharacterBitmap&, Color = Color());
  39. void draw_bitmap(const IntPoint&, const GlyphBitmap&, Color = Color());
  40. void draw_scaled_bitmap(const IntRect& dst_rect, const Gfx::Bitmap&, const IntRect& src_rect, float opacity = 1.0f);
  41. void draw_scaled_bitmap(const IntRect& dst_rect, const Gfx::Bitmap&, const FloatRect& src_rect, float opacity = 1.0f);
  42. void draw_triangle(const IntPoint&, const IntPoint&, const IntPoint&, Color);
  43. void draw_ellipse_intersecting(const IntRect&, Color, int thickness = 1);
  44. void set_pixel(const IntPoint&, Color);
  45. void set_pixel(int x, int y, Color color) { set_pixel({ x, y }, color); }
  46. void draw_line(const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
  47. void draw_quadratic_bezier_curve(const IntPoint& control_point, const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
  48. void draw_elliptical_arc(const IntPoint& p1, const IntPoint& p2, const IntPoint& center, const FloatPoint& radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
  49. void blit(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, float opacity = 1.0f, bool apply_alpha = true);
  50. void blit_dimmed(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect);
  51. void blit_brightened(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect);
  52. void blit_filtered(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, Function<Color(Color)>);
  53. void draw_tiled_bitmap(const IntRect& dst_rect, const Gfx::Bitmap&);
  54. void blit_offset(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, const IntPoint&);
  55. void blit_disabled(const IntPoint&, const Gfx::Bitmap&, const IntRect&, const Palette&);
  56. void blit_tiled(const IntRect&, const Gfx::Bitmap&, const IntRect& src_rect);
  57. void draw_text(const IntRect&, const StringView&, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
  58. void draw_text(const IntRect&, const StringView&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
  59. void draw_text(const IntRect&, const Utf32View&, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
  60. void draw_text(const IntRect&, const Utf32View&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
  61. void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const StringView&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
  62. void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const Utf8View&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
  63. void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const Utf32View&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
  64. void draw_ui_text(const Gfx::IntRect&, const StringView&, const Gfx::Font&, TextAlignment, Gfx::Color);
  65. void draw_glyph(const IntPoint&, u32, Color);
  66. void draw_glyph(const IntPoint&, u32, const Font&, Color);
  67. void draw_emoji(const IntPoint&, const Gfx::Bitmap&, const Font&);
  68. void draw_glyph_or_emoji(const IntPoint&, u32 code_point, const Font&, Color);
  69. void draw_circle_arc_intersecting(const IntRect&, const IntPoint&, int radius, Color, int thickness);
  70. enum class CornerOrientation {
  71. TopLeft,
  72. TopRight,
  73. BottomRight,
  74. BottomLeft
  75. };
  76. void fill_rounded_corner(const IntRect&, int radius, Color, CornerOrientation);
  77. static void for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>&);
  78. static void for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>&&);
  79. static void for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(const FloatPoint&, const FloatPoint&)>&);
  80. static void for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(const FloatPoint&, const FloatPoint&)>&&);
  81. void stroke_path(const Path&, Color, int thickness);
  82. enum class WindingRule {
  83. Nonzero,
  84. EvenOdd,
  85. };
  86. void fill_path(Path&, Color, WindingRule rule = WindingRule::Nonzero);
  87. const Font& font() const { return *state().font; }
  88. void set_font(const Font& font) { state().font = &font; }
  89. enum class DrawOp {
  90. Copy,
  91. Xor,
  92. Invert
  93. };
  94. void set_draw_op(DrawOp op) { state().draw_op = op; }
  95. DrawOp draw_op() const { return state().draw_op; }
  96. void add_clip_rect(const IntRect& rect);
  97. void clear_clip_rect();
  98. void translate(int dx, int dy) { translate({ dx, dy }); }
  99. void translate(const IntPoint& delta) { state().translation.translate_by(delta); }
  100. Gfx::Bitmap* target() { return m_target.ptr(); }
  101. void save() { m_state_stack.append(m_state_stack.last()); }
  102. void restore()
  103. {
  104. VERIFY(m_state_stack.size() > 1);
  105. m_state_stack.take_last();
  106. }
  107. IntRect clip_rect() const { return state().clip_rect; }
  108. protected:
  109. IntPoint translation() const { return state().translation; }
  110. IntRect to_physical(const IntRect& r) const { return r.translated(translation()) * scale(); }
  111. IntPoint to_physical(const IntPoint& p) const { return p.translated(translation()) * scale(); }
  112. int scale() const { return state().scale; }
  113. void set_physical_pixel_with_draw_op(u32& pixel, const Color&);
  114. void fill_physical_scanline_with_draw_op(int y, int x, int width, const Color& color);
  115. void fill_rect_with_draw_op(const IntRect&, Color);
  116. void blit_with_opacity(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, float opacity, bool apply_alpha = true);
  117. void draw_physical_pixel(const IntPoint&, Color, int thickness = 1);
  118. struct State {
  119. const Font* font;
  120. IntPoint translation;
  121. int scale = 1;
  122. IntRect clip_rect;
  123. DrawOp draw_op;
  124. };
  125. State& state() { return m_state_stack.last(); }
  126. const State& state() const { return m_state_stack.last(); }
  127. void fill_physical_rect(const IntRect&, Color);
  128. IntRect m_clip_origin;
  129. NonnullRefPtr<Gfx::Bitmap> m_target;
  130. Vector<State, 4> m_state_stack;
  131. };
  132. class PainterStateSaver {
  133. public:
  134. explicit PainterStateSaver(Painter&);
  135. ~PainterStateSaver();
  136. private:
  137. Painter& m_painter;
  138. };
  139. String parse_ampersand_string(const StringView&, Optional<size_t>* underline_offset = nullptr);
  140. }