AntiAliasingPainter.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGfx/Painter.h>
  8. #include <LibGfx/Path.h>
  9. #include <LibGfx/Quad.h>
  10. namespace Gfx {
  11. class AntiAliasingPainter {
  12. public:
  13. explicit AntiAliasingPainter(Painter& painter)
  14. : m_underlying_painter(painter)
  15. {
  16. }
  17. enum class LineLengthMode {
  18. // E.g. A line from 0,1 -> 2,1 is 3px long
  19. PointToPoint,
  20. // E.g. A line from 0,1 -> 2,1 is 2px long
  21. Distance
  22. };
  23. void draw_line(IntPoint, IntPoint, Color, float thickness = 1, Painter::LineStyle style = Painter::LineStyle::Solid, Color alternate_color = Color::Transparent, LineLengthMode line_length_mode = LineLengthMode::PointToPoint);
  24. void draw_line(FloatPoint, FloatPoint, Color, float thickness = 1, Painter::LineStyle style = Painter::LineStyle::Solid, Color alternate_color = Color::Transparent, LineLengthMode line_length_mode = LineLengthMode::PointToPoint);
  25. void draw_line(FloatLine line, Color color, float thickness = 1, Painter::LineStyle style = Painter::LineStyle::Solid, Color alternate_color = Color::Transparent, LineLengthMode line_length_mode = LineLengthMode::PointToPoint)
  26. {
  27. draw_line(line.a(), line.b(), color, thickness, style, alternate_color, line_length_mode);
  28. }
  29. void fill_path(Path const&, Color, Painter::WindingRule rule = Painter::WindingRule::Nonzero);
  30. void fill_path(Path const&, PaintStyle const& paint_style, float opacity = 1.0f, Painter::WindingRule rule = Painter::WindingRule::Nonzero);
  31. void stroke_path(Path const&, Color, float thickness);
  32. void stroke_path(Path const&, PaintStyle const& paint_style, float thickness, float opacity = 1.0f);
  33. void translate(float dx, float dy) { m_transform.translate(dx, dy); }
  34. void translate(FloatPoint delta) { m_transform.translate(delta); }
  35. void draw_ellipse(IntRect const& a_rect, Color, int thickness);
  36. enum class BlendMode {
  37. Normal,
  38. AlphaSubtract
  39. };
  40. void fill_rect(FloatRect const&, Color);
  41. void fill_circle(IntPoint center, int radius, Color, BlendMode blend_mode = BlendMode::Normal);
  42. void fill_ellipse(IntRect const& a_rect, Color, BlendMode blend_mode = BlendMode::Normal);
  43. void fill_rect_with_rounded_corners(IntRect const&, Color, int radius);
  44. 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);
  45. struct CornerRadius {
  46. int horizontal_radius;
  47. int vertical_radius;
  48. inline operator bool() const
  49. {
  50. return horizontal_radius > 0 || vertical_radius > 0;
  51. }
  52. Gfx::IntRect as_rect() const
  53. {
  54. return { 0, 0, horizontal_radius, vertical_radius };
  55. }
  56. };
  57. void fill_rect_with_rounded_corners(IntRect const&, Color, CornerRadius top_left, CornerRadius top_right, CornerRadius bottom_right, CornerRadius bottom_left, BlendMode blend_mode = BlendMode::Normal);
  58. Gfx::Painter& underlying_painter() { return m_underlying_painter; }
  59. private:
  60. struct Range {
  61. int min;
  62. int max;
  63. inline bool contains_inclusive(int n) const
  64. {
  65. return n >= min && n <= max;
  66. }
  67. };
  68. Range draw_ellipse_part(IntPoint a_rect, int radius_a, int radius_b, Color alternate_color, bool flip_x_and_y, Optional<Range> x_clip, BlendMode blend_mode);
  69. void draw_anti_aliased_line(FloatPoint, FloatPoint, Color, float thickness, Painter::LineStyle, Color, LineLengthMode);
  70. void draw_dotted_line(IntPoint, IntPoint, Gfx::Color, int thickness);
  71. Painter& m_underlying_painter;
  72. AffineTransform m_transform;
  73. };
  74. }