Coverage Report

Created: 2025-03-04 07:22

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