Painter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #include "Color.h"
  3. #include "Point.h"
  4. #include "Rect.h"
  5. #include "Size.h"
  6. #include <AK/String.h>
  7. #include <AK/Utf8View.h>
  8. #include <LibDraw/TextAlignment.h>
  9. #include <LibDraw/TextElision.h>
  10. class CharacterBitmap;
  11. class GlyphBitmap;
  12. class GraphicsBitmap;
  13. class Font;
  14. class Emoji;
  15. class Painter {
  16. public:
  17. explicit Painter(GraphicsBitmap&);
  18. ~Painter();
  19. void clear_rect(const Rect&, Color);
  20. void fill_rect(const Rect&, Color);
  21. void fill_rect_with_gradient(const Rect&, Color gradient_start, Color gradient_end);
  22. void draw_rect(const Rect&, Color, bool rough = false);
  23. void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color());
  24. void draw_bitmap(const Point&, const GlyphBitmap&, Color = Color());
  25. void draw_ellipse_intersecting(const Rect&, Color, int thickness = 1);
  26. void set_pixel(const Point&, Color);
  27. void draw_line(const Point&, const Point&, Color, int thickness = 1, bool dotted = false);
  28. void draw_scaled_bitmap(const Rect& dst_rect, const GraphicsBitmap&, const Rect& src_rect);
  29. void blit(const Point&, const GraphicsBitmap&, const Rect& src_rect, float opacity = 1.0f);
  30. void blit_dimmed(const Point&, const GraphicsBitmap&, const Rect& src_rect);
  31. void draw_tiled_bitmap(const Rect& dst_rect, const GraphicsBitmap&);
  32. void blit_offset(const Point&, const GraphicsBitmap&, const Rect& src_rect, const Point&);
  33. void blit_scaled(const Rect&, const GraphicsBitmap&, const Rect&, float, float);
  34. void draw_text(const Rect&, const StringView&, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
  35. void draw_text(const Rect&, const StringView&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
  36. void draw_glyph(const Point&, char, Color);
  37. void draw_glyph(const Point&, char, const Font&, Color);
  38. void draw_emoji(const Point&, const GraphicsBitmap&, const Font&);
  39. void draw_glyph_or_emoji(const Point&, u32 codepoint, const Font&, Color);
  40. const Font& font() const { return *state().font; }
  41. void set_font(const Font& font) { state().font = &font; }
  42. enum class DrawOp {
  43. Copy,
  44. Xor
  45. };
  46. void set_draw_op(DrawOp op) { state().draw_op = op; }
  47. DrawOp draw_op() const { return state().draw_op; }
  48. void add_clip_rect(const Rect& rect);
  49. void clear_clip_rect();
  50. Rect clip_rect() const { return state().clip_rect; }
  51. void translate(int dx, int dy) { state().translation.move_by(dx, dy); }
  52. void translate(const Point& delta) { state().translation.move_by(delta); }
  53. Point translation() const { return state().translation; }
  54. GraphicsBitmap* target() { return m_target.ptr(); }
  55. void save() { m_state_stack.append(m_state_stack.last()); }
  56. void restore()
  57. {
  58. ASSERT(m_state_stack.size() > 1);
  59. m_state_stack.take_last();
  60. }
  61. protected:
  62. void set_pixel_with_draw_op(u32& pixel, const Color&);
  63. void fill_rect_with_draw_op(const Rect&, Color);
  64. void blit_with_alpha(const Point&, const GraphicsBitmap&, const Rect& src_rect);
  65. void blit_with_opacity(const Point&, const GraphicsBitmap&, const Rect& src_rect, float opacity);
  66. void draw_pixel(const Point&, Color, int thickness = 1);
  67. void draw_text_line(const Rect&, const Utf8View&, const Font&, TextAlignment, Color, TextElision);
  68. struct State {
  69. const Font* font;
  70. Point translation;
  71. Rect clip_rect;
  72. DrawOp draw_op;
  73. };
  74. State& state() { return m_state_stack.last(); }
  75. const State& state() const { return m_state_stack.last(); }
  76. Rect m_clip_origin;
  77. NonnullRefPtr<GraphicsBitmap> m_target;
  78. Vector<State, 4> m_state_stack;
  79. };
  80. class PainterStateSaver {
  81. public:
  82. explicit PainterStateSaver(Painter&);
  83. ~PainterStateSaver();
  84. private:
  85. Painter& m_painter;
  86. };