summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Painting/BorderPainting.cpp
blob: a2151bef8e4b1bcf242692d81e24a7fa22f61ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGfx/Painter.h>
#include <LibWeb/Painting/BorderPainting.h>
#include <LibWeb/Painting/PaintContext.h>

namespace Web::Painting {

void paint_border(PaintContext& context, BorderEdge edge, const Gfx::FloatRect& rect, const CSS::ComputedValues& style)
{
    const auto& border_data = [&] {
        switch (edge) {
        case BorderEdge::Top:
            return style.border_top();
        case BorderEdge::Right:
            return style.border_right();
        case BorderEdge::Bottom:
            return style.border_bottom();
        default: // BorderEdge::Left:
            return style.border_left();
        }
    }();

    float width = border_data.width;
    if (width <= 0)
        return;

    auto color = border_data.color;
    auto border_style = border_data.line_style;
    int int_width = max((int)width, 1);

    struct Points {
        Gfx::FloatPoint p1;
        Gfx::FloatPoint p2;
    };

    auto points_for_edge = [](BorderEdge edge, const Gfx::FloatRect& rect) -> Points {
        switch (edge) {
        case BorderEdge::Top:
            return { rect.top_left(), rect.top_right() };
        case BorderEdge::Right:
            return { rect.top_right(), rect.bottom_right() };
        case BorderEdge::Bottom:
            return { rect.bottom_left(), rect.bottom_right() };
        default: // Edge::Left
            return { rect.top_left(), rect.bottom_left() };
        }
    };

    auto [p1, p2] = points_for_edge(edge, rect);

    if (border_style == CSS::LineStyle::Inset) {
        auto top_left_color = Color::from_rgb(0x5a5a5a);
        auto bottom_right_color = Color::from_rgb(0x888888);
        color = (edge == BorderEdge::Left || edge == BorderEdge::Top) ? top_left_color : bottom_right_color;
    } else if (border_style == CSS::LineStyle::Outset) {
        auto top_left_color = Color::from_rgb(0x888888);
        auto bottom_right_color = Color::from_rgb(0x5a5a5a);
        color = (edge == BorderEdge::Left || edge == BorderEdge::Top) ? top_left_color : bottom_right_color;
    }

    auto gfx_line_style = Gfx::Painter::LineStyle::Solid;
    if (border_style == CSS::LineStyle::Dotted)
        gfx_line_style = Gfx::Painter::LineStyle::Dotted;
    if (border_style == CSS::LineStyle::Dashed)
        gfx_line_style = Gfx::Painter::LineStyle::Dashed;

    if (gfx_line_style != Gfx::Painter::LineStyle::Solid) {
        switch (edge) {
        case BorderEdge::Top:
            p1.move_by(int_width / 2, int_width / 2);
            p2.move_by(-int_width / 2, int_width / 2);
            break;
        case BorderEdge::Right:
            p1.move_by(-int_width / 2, int_width / 2);
            p2.move_by(-int_width / 2, -int_width / 2);
            break;
        case BorderEdge::Bottom:
            p1.move_by(int_width / 2, -int_width / 2);
            p2.move_by(-int_width / 2, -int_width / 2);
            break;
        case BorderEdge::Left:
            p1.move_by(int_width / 2, int_width / 2);
            p2.move_by(int_width / 2, -int_width / 2);
            break;
        }
        context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, int_width, gfx_line_style);
        return;
    }

    auto draw_line = [&](auto& p1, auto& p2) {
        context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1, gfx_line_style);
    };

    float p1_step = 0;
    float p2_step = 0;

    switch (edge) {
    case BorderEdge::Top:
        p1_step = style.border_left().width / (float)int_width;
        p2_step = style.border_right().width / (float)int_width;
        for (int i = 0; i < int_width; ++i) {
            draw_line(p1, p2);
            p1.move_by(p1_step, 1);
            p2.move_by(-p2_step, 1);
        }
        break;
    case BorderEdge::Right:
        p1_step = style.border_top().width / (float)int_width;
        p2_step = style.border_bottom().width / (float)int_width;
        for (int i = int_width - 1; i >= 0; --i) {
            draw_line(p1, p2);
            p1.move_by(-1, p1_step);
            p2.move_by(-1, -p2_step);
        }
        break;
    case BorderEdge::Bottom:
        p1_step = style.border_left().width / (float)int_width;
        p2_step = style.border_right().width / (float)int_width;
        for (int i = int_width - 1; i >= 0; --i) {
            draw_line(p1, p2);
            p1.move_by(p1_step, -1);
            p2.move_by(-p2_step, -1);
        }
        break;
    case BorderEdge::Left:
        p1_step = style.border_top().width / (float)int_width;
        p2_step = style.border_bottom().width / (float)int_width;
        for (int i = 0; i < int_width; ++i) {
            draw_line(p1, p2);
            p1.move_by(1, p1_step);
            p2.move_by(1, -p2_step);
        }
        break;
    }
}

}