| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |  * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/CSS/Length.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-30 17:13:37 +01:00
										 |  |  | #include <LibWeb/CSS/PercentageOr.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Size { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     enum class Type { | 
					
						
							|  |  |  |         Auto, | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |         Calculated, | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |         Length, | 
					
						
							|  |  |  |         Percentage, | 
					
						
							|  |  |  |         MinContent, | 
					
						
							|  |  |  |         MaxContent, | 
					
						
							|  |  |  |         FitContent, | 
					
						
							|  |  |  |         None, // NOTE: This is only valid for max-width and max-height.
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static Size make_auto(); | 
					
						
							| 
									
										
										
										
											2022-11-03 14:43:24 +00:00
										 |  |  |     static Size make_px(CSSPixels); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     static Size make_length(Length); | 
					
						
							|  |  |  |     static Size make_percentage(Percentage); | 
					
						
							| 
									
										
										
										
											2024-09-18 17:27:47 +01:00
										 |  |  |     static Size make_calculated(NonnullRefPtr<CSSMathValue>); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     static Size make_min_content(); | 
					
						
							|  |  |  |     static Size make_max_content(); | 
					
						
							|  |  |  |     static Size make_fit_content(Length available_space); | 
					
						
							| 
									
										
										
										
											2023-05-26 18:58:06 +02:00
										 |  |  |     static Size make_fit_content(); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     static Size make_none(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_auto() const { return m_type == Type::Auto; } | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |     bool is_calculated() const { return m_type == Type::Calculated; } | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     bool is_length() const { return m_type == Type::Length; } | 
					
						
							|  |  |  |     bool is_percentage() const { return m_type == Type::Percentage; } | 
					
						
							|  |  |  |     bool is_min_content() const { return m_type == Type::MinContent; } | 
					
						
							|  |  |  |     bool is_max_content() const { return m_type == Type::MaxContent; } | 
					
						
							|  |  |  |     bool is_fit_content() const { return m_type == Type::FitContent; } | 
					
						
							|  |  |  |     bool is_none() const { return m_type == Type::None; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-06 16:33:06 +02:00
										 |  |  |     [[nodiscard]] CSSPixels to_px(Layout::Node const&, CSSPixels reference_value) const; | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool contains_percentage() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 17:27:47 +01:00
										 |  |  |     CSSMathValue const& calculated() const | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_calculated()); | 
					
						
							|  |  |  |         return m_length_percentage.calculated(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     CSS::Length const& length() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_length()); | 
					
						
							|  |  |  |         return m_length_percentage.length(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CSS::Percentage const& percentage() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_percentage()); | 
					
						
							|  |  |  |         return m_length_percentage.percentage(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CSS::Length const& fit_content_available_space() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_fit_content()); | 
					
						
							|  |  |  |         return m_length_percentage.length(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  |     String to_string() const; | 
					
						
							| 
									
										
										
										
											2022-09-26 11:14:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     Size(Type type, LengthPercentage); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Type m_type {}; | 
					
						
							|  |  |  |     CSS::LengthPercentage m_length_percentage; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-09-26 11:14:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<Web::CSS::Size> : Formatter<StringView> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Size const& size) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  |         return Formatter<StringView>::format(builder, size.to_string()); | 
					
						
							| 
									
										
										
										
											2022-09-26 11:14:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |