| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:33:37 +01:00
										 |  |  |  * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Angle.h"
 | 
					
						
							|  |  |  | #include <AK/Math.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:33:37 +01:00
										 |  |  | #include <LibWeb/CSS/Percentage.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-11 15:05:56 +00:00
										 |  |  | #include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | Angle::Angle(double value, Type type) | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |     : m_type(type) | 
					
						
							|  |  |  |     , m_value(value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | Angle Angle::make_degrees(double value) | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     return { value, Type::Deg }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Angle Angle::percentage_of(Percentage const& percentage) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Angle { percentage.as_fraction() * m_value, m_type }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  | String Angle::to_string() const | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  |     return MUST(String::formatted("{}deg", to_degrees())); | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | double Angle::to_degrees() const | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Deg: | 
					
						
							|  |  |  |         return m_value; | 
					
						
							|  |  |  |     case Type::Grad: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |         return m_value * (360.0 / 400.0); | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |     case Type::Rad: | 
					
						
							| 
									
										
										
										
											2023-09-09 14:43:39 +02:00
										 |  |  |         return AK::to_degrees(m_value); | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |     case Type::Turn: | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |         return m_value * 360.0; | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 23:50:33 +02:00
										 |  |  | double Angle::to_radians() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-09 14:43:39 +02:00
										 |  |  |     return AK::to_radians(to_degrees()); | 
					
						
							| 
									
										
										
										
											2023-05-27 23:50:33 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | StringView Angle::unit_name() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Deg: | 
					
						
							|  |  |  |         return "deg"sv; | 
					
						
							|  |  |  |     case Type::Grad: | 
					
						
							|  |  |  |         return "grad"sv; | 
					
						
							|  |  |  |     case Type::Rad: | 
					
						
							|  |  |  |         return "rad"sv; | 
					
						
							|  |  |  |     case Type::Turn: | 
					
						
							|  |  |  |         return "turn"sv; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<Angle::Type> Angle::unit_from_name(StringView name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     if (name.equals_ignoring_ascii_case("deg"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |         return Type::Deg; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (name.equals_ignoring_ascii_case("grad"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |         return Type::Grad; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (name.equals_ignoring_ascii_case("rad"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |         return Type::Rad; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (name.equals_ignoring_ascii_case("turn"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  |         return Type::Turn; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 15:05:56 +00:00
										 |  |  | Angle Angle::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const&, Angle const& reference_value) | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return calculated->resolve_angle_percentage(reference_value).value(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 17:43:30 +00:00
										 |  |  | } |