| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |  * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> | 
					
						
							| 
									
										
										
										
											2025-02-21 16:45:07 +00:00
										 |  |  |  * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |  * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "LinearGradientStyleValue.h"
 | 
					
						
							| 
									
										
										
										
											2025-02-21 16:45:07 +00:00
										 |  |  | #include <LibWeb/Layout/Node.h>
 | 
					
						
							| 
									
										
										
										
											2025-02-02 15:55:26 +01:00
										 |  |  | #include <LibWeb/Painting/DisplayListRecorder.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 00:59:49 +01:00
										 |  |  | String LinearGradientStyleValue::to_string(SerializationMode mode) const | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     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: | 
					
						
							| 
									
										
										
										
											2025-02-19 21:02:12 +11:00
										 |  |  |             return "left top"sv; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |         case SideOrCorner::TopRight: | 
					
						
							| 
									
										
										
										
											2025-02-19 21:02:12 +11:00
										 |  |  |             return "right top"sv; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |         case SideOrCorner::BottomLeft: | 
					
						
							| 
									
										
										
										
											2025-02-19 21:02:12 +11:00
										 |  |  |             return "left bottom"sv; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |         case SideOrCorner::BottomRight: | 
					
						
							| 
									
										
										
										
											2025-02-19 21:02:12 +11:00
										 |  |  |             return "right bottom"sv; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |         default: | 
					
						
							|  |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 21:02:12 +11:00
										 |  |  |     auto default_direction = m_properties.gradient_type == GradientType::WebKit ? SideOrCorner::Top : SideOrCorner::Bottom; | 
					
						
							|  |  |  |     bool has_direction = m_properties.direction != default_direction; | 
					
						
							|  |  |  |     bool has_color_space = m_properties.interpolation_method.has_value() && m_properties.interpolation_method.value().color_space != InterpolationMethod::default_color_space(m_properties.color_syntax); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |     if (m_properties.gradient_type == GradientType::WebKit) | 
					
						
							| 
									
										
										
										
											2023-08-22 14:08:15 +01:00
										 |  |  |         builder.append("-webkit-"sv); | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |     if (is_repeating()) | 
					
						
							| 
									
										
										
										
											2023-08-22 14:08:15 +01:00
										 |  |  |         builder.append("repeating-"sv); | 
					
						
							|  |  |  |     builder.append("linear-gradient("sv); | 
					
						
							| 
									
										
										
										
											2025-02-19 21:02:12 +11:00
										 |  |  |     if (has_direction) { | 
					
						
							|  |  |  |         m_properties.direction.visit( | 
					
						
							|  |  |  |             [&](SideOrCorner side_or_corner) { | 
					
						
							| 
									
										
										
										
											2025-04-08 13:50:50 -04:00
										 |  |  |                 builder.appendff("{}{}", m_properties.gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner)); | 
					
						
							| 
									
										
										
										
											2025-02-19 21:02:12 +11:00
										 |  |  |             }, | 
					
						
							|  |  |  |             [&](Angle const& angle) { | 
					
						
							|  |  |  |                 builder.append(angle.to_string()); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (has_color_space) | 
					
						
							|  |  |  |             builder.append(' '); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (has_color_space) | 
					
						
							|  |  |  |         builder.append(m_properties.interpolation_method.value().to_string()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (has_direction || has_color_space) | 
					
						
							|  |  |  |         builder.append(", "sv); | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 00:59:49 +01:00
										 |  |  |     serialize_color_stop_list(builder, m_properties.color_stop_list, mode); | 
					
						
							| 
									
										
										
										
											2023-08-22 14:08:15 +01:00
										 |  |  |     builder.append(")"sv); | 
					
						
							|  |  |  |     return MUST(builder.to_string()); | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-08 10:11:51 +01:00
										 |  |  | bool LinearGradientStyleValue::equals(StyleValue const& other_) const | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     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 = [&] { | 
					
						
							| 
									
										
										
										
											2023-09-09 14:43:39 +02:00
										 |  |  |         return AK::to_degrees(atan2(gradient_size.height().to_double(), gradient_size.width().to_double())); | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  |     return m_properties.direction.visit( | 
					
						
							|  |  |  |         [&](SideOrCorner side_or_corner) { | 
					
						
							|  |  |  |             auto angle = [&] { | 
					
						
							|  |  |  |                 switch (side_or_corner) { | 
					
						
							|  |  |  |                 case SideOrCorner::Top: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |                     return 0.0; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |                 case SideOrCorner::Bottom: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |                     return 180.0; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |                 case SideOrCorner::Left: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |                     return 270.0; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |                 case SideOrCorner::Right: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |                     return 90.0; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |                 case SideOrCorner::TopRight: | 
					
						
							|  |  |  |                     return corner_angle_degrees(); | 
					
						
							|  |  |  |                 case SideOrCorner::BottomLeft: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |                     return corner_angle_degrees() + 180.0; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |                 case SideOrCorner::TopLeft: | 
					
						
							|  |  |  |                     return -corner_angle_degrees(); | 
					
						
							|  |  |  |                 case SideOrCorner::BottomRight: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |                     return -(corner_angle_degrees() + 180.0); | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |                 default: | 
					
						
							|  |  |  |                     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }(); | 
					
						
							|  |  |  |             // Note: For unknowable reasons the angles are opposite on the -webkit- version
 | 
					
						
							|  |  |  |             if (m_properties.gradient_type == GradientType::WebKit) | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |                 return angle + 180.0; | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  |             return angle; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         [&](Angle const& angle) { | 
					
						
							|  |  |  |             return angle.to_degrees(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-18 16:13:37 +00:00
										 |  |  | void LinearGradientStyleValue::resolve_for_size(Layout::NodeWithStyle const& node, CSSPixelSize size) const | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-21 16:45:07 +00:00
										 |  |  |     ResolvedDataCacheKey cache_key { | 
					
						
							|  |  |  |         .length_resolution_context = Length::ResolutionContext::for_layout_node(node), | 
					
						
							|  |  |  |         .size = size, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     if (m_resolved_data_cache_key != cache_key) { | 
					
						
							|  |  |  |         m_resolved_data_cache_key = move(cache_key); | 
					
						
							|  |  |  |         m_resolved = Painting::resolve_linear_gradient_data(node, size, *this); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-31 23:07:26 +02:00
										 |  |  | void LinearGradientStyleValue::paint(DisplayListRecordingContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     VERIFY(m_resolved.has_value()); | 
					
						
							| 
									
										
										
										
											2025-02-21 16:45:07 +00:00
										 |  |  |     context.display_list_recorder().fill_rect_with_linear_gradient(dest_rect.to_type<int>(), m_resolved.value()); | 
					
						
							| 
									
										
										
										
											2023-03-24 16:17:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |