mirror of
				https://github.com/LadybirdBrowser/ladybird.git
				synced 2025-11-04 07:10:57 +00:00 
			
		
		
		
	Although DistinctNumeric, which is supposed to abstract the underlying type, was used to represent CSSPixels, we have a whole bunch of places in the layout code that assume CSSPixels::value() returns a floating-point type. This assumption makes it difficult to replace the underlying type in CSSPixels with a non-floating type. To make it easier to transition CSSPixels to fixed-point math, one step we can take is to prevent access to the underlying type using value() and instead use explicit conversions with the to_float(), to_double(), and to_int() methods.
		
			
				
	
	
		
			118 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 | 
						|
 * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
 | 
						|
 * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
 | 
						|
 * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
 | 
						|
 *
 | 
						|
 * SPDX-License-Identifier: BSD-2-Clause
 | 
						|
 */
 | 
						|
 | 
						|
#include "LinearGradientStyleValue.h"
 | 
						|
 | 
						|
namespace Web::CSS {
 | 
						|
 | 
						|
ErrorOr<String> LinearGradientStyleValue::to_string() const
 | 
						|
{
 | 
						|
    StringBuilder builder;
 | 
						|
    auto side_or_corner_to_string = [](SideOrCorner value) {
 | 
						|
        switch (value) {
 | 
						|
        case SideOrCorner::Top:
 | 
						|
            return "top"sv;
 | 
						|
        case SideOrCorner::Bottom:
 | 
						|
            return "bottom"sv;
 | 
						|
        case SideOrCorner::Left:
 | 
						|
            return "left"sv;
 | 
						|
        case SideOrCorner::Right:
 | 
						|
            return "right"sv;
 | 
						|
        case SideOrCorner::TopLeft:
 | 
						|
            return "top left"sv;
 | 
						|
        case SideOrCorner::TopRight:
 | 
						|
            return "top right"sv;
 | 
						|
        case SideOrCorner::BottomLeft:
 | 
						|
            return "bottom left"sv;
 | 
						|
        case SideOrCorner::BottomRight:
 | 
						|
            return "bottom right"sv;
 | 
						|
        default:
 | 
						|
            VERIFY_NOT_REACHED();
 | 
						|
        }
 | 
						|
    };
 | 
						|
 | 
						|
    if (m_properties.gradient_type == GradientType::WebKit)
 | 
						|
        TRY(builder.try_append("-webkit-"sv));
 | 
						|
    if (is_repeating())
 | 
						|
        TRY(builder.try_append("repeating-"sv));
 | 
						|
    TRY(builder.try_append("linear-gradient("sv));
 | 
						|
    TRY(m_properties.direction.visit(
 | 
						|
        [&](SideOrCorner side_or_corner) -> ErrorOr<void> {
 | 
						|
            return builder.try_appendff("{}{}, "sv, m_properties.gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner));
 | 
						|
        },
 | 
						|
        [&](Angle const& angle) -> ErrorOr<void> {
 | 
						|
            return builder.try_appendff("{}, "sv, TRY(angle.to_string()));
 | 
						|
        }));
 | 
						|
 | 
						|
    TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
 | 
						|
    TRY(builder.try_append(")"sv));
 | 
						|
    return builder.to_string();
 | 
						|
}
 | 
						|
 | 
						|
bool LinearGradientStyleValue::equals(StyleValue const& other_) const
 | 
						|
{
 | 
						|
    if (type() != other_.type())
 | 
						|
        return false;
 | 
						|
    auto& other = other_.as_linear_gradient();
 | 
						|
    return m_properties == other.m_properties;
 | 
						|
}
 | 
						|
 | 
						|
float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const
 | 
						|
{
 | 
						|
    auto corner_angle_degrees = [&] {
 | 
						|
        return atan2(gradient_size.height().to_double(), gradient_size.width().to_double()) * 180 / AK::Pi<double>;
 | 
						|
    };
 | 
						|
    return m_properties.direction.visit(
 | 
						|
        [&](SideOrCorner side_or_corner) {
 | 
						|
            auto angle = [&] {
 | 
						|
                switch (side_or_corner) {
 | 
						|
                case SideOrCorner::Top:
 | 
						|
                    return 0.0;
 | 
						|
                case SideOrCorner::Bottom:
 | 
						|
                    return 180.0;
 | 
						|
                case SideOrCorner::Left:
 | 
						|
                    return 270.0;
 | 
						|
                case SideOrCorner::Right:
 | 
						|
                    return 90.0;
 | 
						|
                case SideOrCorner::TopRight:
 | 
						|
                    return corner_angle_degrees();
 | 
						|
                case SideOrCorner::BottomLeft:
 | 
						|
                    return corner_angle_degrees() + 180.0;
 | 
						|
                case SideOrCorner::TopLeft:
 | 
						|
                    return -corner_angle_degrees();
 | 
						|
                case SideOrCorner::BottomRight:
 | 
						|
                    return -(corner_angle_degrees() + 180.0);
 | 
						|
                default:
 | 
						|
                    VERIFY_NOT_REACHED();
 | 
						|
                }
 | 
						|
            }();
 | 
						|
            // Note: For unknowable reasons the angles are opposite on the -webkit- version
 | 
						|
            if (m_properties.gradient_type == GradientType::WebKit)
 | 
						|
                return angle + 180.0;
 | 
						|
            return angle;
 | 
						|
        },
 | 
						|
        [&](Angle const& angle) {
 | 
						|
            return angle.to_degrees();
 | 
						|
        });
 | 
						|
}
 | 
						|
 | 
						|
void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
 | 
						|
{
 | 
						|
    if (m_resolved.has_value() && m_resolved->size == size)
 | 
						|
        return;
 | 
						|
    m_resolved = ResolvedData { Painting::resolve_linear_gradient_data(node, size, *this), size };
 | 
						|
}
 | 
						|
 | 
						|
void LinearGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
 | 
						|
{
 | 
						|
    VERIFY(m_resolved.has_value());
 | 
						|
    Painting::paint_linear_gradient(context, dest_rect, m_resolved->data);
 | 
						|
}
 | 
						|
 | 
						|
}
 |