Coverage Report

Created: 2025-09-05 06:52

/src/serenity/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/Math.h>
8
#include <LibGfx/Gradients.h>
9
#include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
10
#include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
11
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
12
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
13
#include <LibWeb/Layout/Node.h>
14
#include <LibWeb/Painting/GradientPainting.h>
15
16
namespace Web::Painting {
17
18
static ColorStopData resolve_color_stop_positions(Layout::NodeWithStyleAndBoxModelMetrics const& node, auto const& color_stop_list, auto resolve_position_to_float, bool repeating)
19
0
{
20
0
    VERIFY(color_stop_list.size() >= 2);
21
0
    ColorStopList resolved_color_stops;
22
23
0
    auto color_stop_length = [&](auto& stop) {
24
0
        return stop.color_stop.second_position.has_value() ? 2 : 1;
25
0
    };
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0, bool)::{lambda(auto:1&)#1}::operator()<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage> const>(Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage> const&) const
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul>, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul> const&, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0, bool)::{lambda(auto:1&)#1}::operator()<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage> const>(Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage> const&) const
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0, bool)::{lambda(auto:1&)#1}::operator()<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage> const>(Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage> const&) const
26
27
0
    size_t expanded_size = 0;
28
0
    for (auto& stop : color_stop_list)
29
0
        expanded_size += color_stop_length(stop);
30
31
0
    resolved_color_stops.ensure_capacity(expanded_size);
32
0
    for (auto& stop : color_stop_list) {
33
0
        auto resolved_stop = Gfx::ColorStop { .color = stop.color_stop.color->to_color(node) };
34
0
        for (int i = 0; i < color_stop_length(stop); i++)
35
0
            resolved_color_stops.append(resolved_stop);
36
0
    }
37
38
    // 1. If the first color stop does not have a position, set its position to 0%.
39
0
    resolved_color_stops.first().position = 0;
40
    //    If the last color stop does not have a position, set its position to 100%
41
0
    resolved_color_stops.last().position = 1.0f;
42
43
    // 2. If a color stop or transition hint has a position that is less than the
44
    //    specified position of any color stop or transition hint before it in the list,
45
    //    set its position to be equal to the largest specified position of any color stop
46
    //    or transition hint before it.
47
0
    auto max_previous_color_stop_or_hint = resolved_color_stops[0].position;
48
0
    auto resolve_stop_position = [&](auto& position) {
49
0
        float value = resolve_position_to_float(position);
50
0
        value = max(value, max_previous_color_stop_or_hint);
51
0
        max_previous_color_stop_or_hint = value;
52
0
        return value;
53
0
    };
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0, bool)::{lambda(auto:1&)#2}::operator()<Web::CSS::LengthPercentage const>(Web::CSS::LengthPercentage const&) const
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul>, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul> const&, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0, bool)::{lambda(auto:1&)#2}::operator()<Web::CSS::AnglePercentage const>(Web::CSS::AnglePercentage const&) const
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0, bool)::{lambda(auto:1&)#2}::operator()<Web::CSS::LengthPercentage const>(Web::CSS::LengthPercentage const&) const
54
0
    size_t resolved_index = 0;
55
0
    for (auto& stop : color_stop_list) {
56
0
        if (stop.transition_hint.has_value())
57
0
            resolved_color_stops[resolved_index].transition_hint = resolve_stop_position(stop.transition_hint->value);
58
0
        if (stop.color_stop.position.has_value())
59
0
            resolved_color_stops[resolved_index].position = resolve_stop_position(*stop.color_stop.position);
60
0
        if (stop.color_stop.second_position.has_value())
61
0
            resolved_color_stops[++resolved_index].position = resolve_stop_position(*stop.color_stop.second_position);
62
0
        ++resolved_index;
63
0
    }
64
65
    // 3. If any color stop still does not have a position, then, for each run of adjacent color stops
66
    //    without positions, set their positions so that they are evenly spaced between the preceding
67
    //    and following color stops with positions.
68
    // Note: Though not mentioned anywhere in the specification transition hints are counted as "color stops with positions".
69
0
    size_t i = 1;
70
0
    auto find_run_end = [&] {
71
0
        auto color_stop_has_position = [](auto& color_stop) {
72
0
            return color_stop.transition_hint.has_value() || isfinite(color_stop.position);
73
0
        };
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0, bool)::{lambda()#1}::operator()() const::{lambda(auto:1&)#1}::operator()<Gfx::ColorStop>({lambda()#1}) const
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul>, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul> const&, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0, bool)::{lambda()#1}::operator()() const::{lambda(auto:1&)#1}::operator()<Gfx::ColorStop>({lambda()#1}) const
Unexecuted instantiation: GradientPainting.cpp:auto Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0, bool)::{lambda()#1}::operator()() const::{lambda(auto:1&)#1}::operator()<Gfx::ColorStop>({lambda()#1}) const
74
0
        while (i < color_stop_list.size() - 1 && !color_stop_has_position(resolved_color_stops[i])) {
75
0
            i++;
76
0
        }
77
0
        return i;
78
0
    };
Unexecuted instantiation: GradientPainting.cpp:Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0, bool)::{lambda()#1}::operator()() const
Unexecuted instantiation: GradientPainting.cpp:Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul>, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul> const&, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0, bool)::{lambda()#1}::operator()() const
Unexecuted instantiation: GradientPainting.cpp:Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0, bool)::{lambda()#1}::operator()() const
79
0
    while (i < resolved_color_stops.size() - 1) {
80
0
        auto& stop = resolved_color_stops[i];
81
0
        if (!isfinite(stop.position)) {
82
0
            auto run_start = i - 1;
83
0
            auto start_position = resolved_color_stops[i++].transition_hint.value_or(resolved_color_stops[run_start].position);
84
0
            auto run_end = find_run_end();
85
0
            auto end_position = resolved_color_stops[run_end].transition_hint.value_or(resolved_color_stops[run_end].position);
86
0
            auto spacing = (end_position - start_position) / (run_end - run_start);
87
0
            for (auto j = run_start + 1; j < run_end; j++) {
88
0
                resolved_color_stops[j].position = start_position + (j - run_start) * spacing;
89
0
            }
90
0
        }
91
0
        i++;
92
0
    }
93
94
    // Determine the location of the transition hint as a percentage of the distance between the two color stops,
95
    // denoted as a number between 0 and 1, where 0 indicates the hint is placed right on the first color stop,
96
    // and 1 indicates the hint is placed right on the second color stop.
97
0
    for (size_t i = 1; i < resolved_color_stops.size(); i++) {
98
0
        auto& color_stop = resolved_color_stops[i];
99
0
        auto& previous_color_stop = resolved_color_stops[i - 1];
100
0
        if (color_stop.transition_hint.has_value()) {
101
0
            auto stop_length = color_stop.position - previous_color_stop.position;
102
0
            color_stop.transition_hint = stop_length > 0 ? (*color_stop.transition_hint - previous_color_stop.position) / stop_length : 0;
103
0
        }
104
0
    }
105
106
0
    Optional<float> repeat_length = {};
107
0
    if (repeating)
108
0
        repeat_length = resolved_color_stops.last().position - resolved_color_stops.first().position;
109
110
0
    return { resolved_color_stops, repeat_length };
111
0
}
Unexecuted instantiation: GradientPainting.cpp:Web::Painting::ColorStopData Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_linear_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::LinearGradientStyleValue const&)::$_0, bool)
Unexecuted instantiation: GradientPainting.cpp:Web::Painting::ColorStopData Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul>, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::AnglePercentage>, 0ul> const&, Web::Painting::resolve_conic_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Web::CSS::ConicGradientStyleValue const&)::$_0, bool)
Unexecuted instantiation: GradientPainting.cpp:Web::Painting::ColorStopData Web::Painting::resolve_color_stop_positions<AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul>, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0>(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, AK::Vector<Web::CSS::ColorStopListElement<Web::CSS::LengthPercentage>, 0ul> const&, Web::Painting::resolve_radial_gradient_data(Web::Layout::NodeWithStyleAndBoxModelMetrics const&, Gfx::Size<Web::CSSPixels>, Web::CSS::RadialGradientStyleValue const&)::$_0, bool)
112
113
LinearGradientData resolve_linear_gradient_data(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSSPixelSize gradient_size, CSS::LinearGradientStyleValue const& linear_gradient)
114
0
{
115
0
    auto gradient_angle = linear_gradient.angle_degrees(gradient_size);
116
0
    auto gradient_length_px = Gfx::calculate_gradient_length(gradient_size.to_type<float>(), gradient_angle);
117
118
0
    auto resolved_color_stops = resolve_color_stop_positions(
119
0
        node, linear_gradient.color_stop_list(), [&](auto const& length_percentage) {
120
0
            return length_percentage.to_px(node, CSSPixels::nearest_value_for(gradient_length_px)).to_float() / static_cast<float>(gradient_length_px);
121
0
        },
122
0
        linear_gradient.is_repeating());
123
124
0
    return { gradient_angle, resolved_color_stops };
125
0
}
126
127
ConicGradientData resolve_conic_gradient_data(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSS::ConicGradientStyleValue const& conic_gradient)
128
0
{
129
0
    CSS::Angle one_turn(360.0f, CSS::Angle::Type::Deg);
130
0
    auto resolved_color_stops = resolve_color_stop_positions(
131
0
        node, conic_gradient.color_stop_list(), [&](auto const& angle_percentage) {
132
0
            return angle_percentage.resolved(node, one_turn).to_degrees() / one_turn.to_degrees();
133
0
        },
134
0
        conic_gradient.is_repeating());
135
0
    return { conic_gradient.angle_degrees(), resolved_color_stops };
136
0
}
137
138
RadialGradientData resolve_radial_gradient_data(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSSPixelSize gradient_size, CSS::RadialGradientStyleValue const& radial_gradient)
139
0
{
140
    // Start center, goes right to ending point, where the gradient line intersects the ending shape
141
0
    auto resolved_color_stops = resolve_color_stop_positions(
142
0
        node, radial_gradient.color_stop_list(), [&](auto const& length_percentage) {
143
0
            return length_percentage.to_px(node, gradient_size.width()).to_float() / gradient_size.width().to_float();
144
0
        },
145
0
        radial_gradient.is_repeating());
146
0
    return { resolved_color_stops };
147
0
}
148
149
}