| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:54:38 +01:00
										 |  |  |  * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Frequency.h"
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:54:38 +01:00
										 |  |  | #include <LibWeb/CSS/Percentage.h>
 | 
					
						
							| 
									
										
										
										
											2024-09-18 17:27:47 +01:00
										 |  |  | #include <LibWeb/CSS/StyleValues/CSSMathValue.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | Frequency::Frequency(double value, Type type) | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  |     : m_type(type) | 
					
						
							|  |  |  |     , m_value(value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | Frequency Frequency::make_hertz(double value) | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     return { value, Type::Hz }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Frequency Frequency::percentage_of(Percentage const& percentage) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Frequency { percentage.as_fraction() * m_value, m_type }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  | String Frequency::to_string() const | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  |     return MUST(String::formatted("{}hz", to_hertz())); | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | double Frequency::to_hertz() const | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Hz: | 
					
						
							|  |  |  |         return m_value; | 
					
						
							|  |  |  |     case Type::kHz: | 
					
						
							|  |  |  |         return m_value * 1000; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | StringView Frequency::unit_name() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Hz: | 
					
						
							|  |  |  |         return "hz"sv; | 
					
						
							|  |  |  |     case Type::kHz: | 
					
						
							|  |  |  |         return "khz"sv; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<Frequency::Type> Frequency::unit_from_name(StringView name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     if (name.equals_ignoring_ascii_case("hz"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  |         return Type::Hz; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("khz"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  |         return Type::kHz; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 17:27:47 +01:00
										 |  |  | Frequency Frequency::resolve_calculated(NonnullRefPtr<CSSMathValue> const& calculated, Layout::Node const&, Frequency const& reference_value) | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return calculated->resolve_frequency_percentage(reference_value).value(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 17:49:47 +00:00
										 |  |  | } |