AntiAliasingPainter.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 draw_line_for_path(FloatPoint, FloatPoint, Color, float thickness = 1, Painter::LineStyle style = Painter::LineStyle::Solid, Color alternate_color = Color::Transparent, LineLengthMode line_length_mode = LineLengthMode::PointToPoint);
  30. void fill_path(Path const&, Color, Painter::WindingRule rule = Painter::WindingRule::Nonzero);
  31. void fill_path(Path const&, PaintStyle const& paint_style, Painter::WindingRule rule = Painter::WindingRule::Nonzero);
  32. void stroke_path(Path const&, Color, float thickness);
  33. void draw_quadratic_bezier_curve(FloatPoint control_point, FloatPoint, FloatPoint, Color, float thickness = 1, Painter::LineStyle style = Painter::LineStyle::Solid);
  34. void draw_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint, FloatPoint, Color, float thickness = 1, Painter::LineStyle style = Painter::LineStyle::Solid);
  35. void draw_elliptical_arc(FloatPoint p1, FloatPoint p2, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, Color, float thickness = 1, Painter::LineStyle style = Painter::LineStyle::Solid);
  36. void translate(float dx, float dy) { m_transform.translate(dx, dy); }
  37. void translate(FloatPoint delta) { m_transform.translate(delta); }
  38. void draw_ellipse(IntRect const& a_rect, Color, int thickness);
  39. enum class BlendMode {
  40. Normal,
  41. AlphaSubtract
  42. };
  43. void fill_rect(FloatRect const&, Color);
  44. void fill_circle(IntPoint center, int radius, Color, BlendMode blend_mode = BlendMode::Normal);
  45. void fill_ellipse(IntRect const& a_rect, Color, BlendMode blend_mode = BlendMode::Normal);
  46. void fill_rect_with_rounded_corners(IntRect const&, Color, int radius);
  47. 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);
  48. struct CornerRadius {
  49. int horizontal_radius;
  50. int vertical_radius;
  51. inline operator bool() const
  52. {
  53. return horizontal_radius > 0 && vertical_radius > 0;
  54. }
  55. Gfx::IntRect as_rect() const
  56. {
  57. return { 0, 0, horizontal_radius, vertical_radius };
  58. }
  59. };
  60. 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);
  61. Gfx::Painter& underlying_painter() { return m_underlying_painter; }
  62. private:
  63. struct Range {
  64. int min;
  65. int max;
  66. inline bool contains_inclusive(int n) const
  67. {
  68. return n >= min && n <= max;
  69. }
  70. };
  71. Range draw_ellipse_part(IntPoint a_rect, int radius_a, int radius_b, Color, bool flip_x_and_y, Optional<Range> x_clip, BlendMode blend_mode);
  72. void draw_dotted_line(IntPoint, IntPoint, Gfx::Color, int thickness);
  73. enum class FixmeEnableHacksForBetterPathPainting {
  74. Yes,
  75. No,
  76. };
  77. template<FixmeEnableHacksForBetterPathPainting path_hacks>
  78. void draw_anti_aliased_line(FloatPoint, FloatPoint, Color, float thickness, Painter::LineStyle style, Color alternate_color, LineLengthMode line_length_mode = LineLengthMode::PointToPoint);
  79. void stroke_segment_intersection(FloatPoint current_line_a, FloatPoint current_line_b, FloatLine const& previous_line, Color, float thickness);
  80. Painter& m_underlying_painter;
  81. AffineTransform m_transform;
  82. };
  83. }