| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-06 15:34:26 +02:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-03 07:55:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 20:01:35 +02:00
										 |  |  | namespace Web::CSS { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | class Length { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     enum class Type { | 
					
						
							| 
									
										
										
										
											2020-06-24 11:24:00 +02:00
										 |  |  |         Undefined, | 
					
						
							| 
									
										
										
										
											2020-06-24 15:38:21 +02:00
										 |  |  |         Percentage, | 
					
						
							| 
									
										
										
										
											2021-06-12 00:03:15 +02:00
										 |  |  |         Calculated, | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  |         Auto, | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |         Cm, | 
					
						
							|  |  |  |         In, | 
					
						
							|  |  |  |         Mm, | 
					
						
							|  |  |  |         Q, | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |         Px, | 
					
						
							| 
									
										
										
										
											2020-06-28 15:25:32 +02:00
										 |  |  |         Pt, | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |         Pc, | 
					
						
							| 
									
										
										
										
											2020-08-07 20:30:27 +02:00
										 |  |  |         Ex, | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |         Em, | 
					
						
							|  |  |  |         Rem, | 
					
						
							| 
									
										
										
										
											2020-09-08 20:39:09 +02:00
										 |  |  |         Vh, | 
					
						
							|  |  |  |         Vw, | 
					
						
							|  |  |  |         Vmax, | 
					
						
							|  |  |  |         Vmin, | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 11:24:00 +02:00
										 |  |  |     Length() { } | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  |     Length(int value, Type type) | 
					
						
							|  |  |  |         : m_type(type) | 
					
						
							|  |  |  |         , m_value(value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-18 16:25:38 +01:00
										 |  |  |     Length(float value, Type type) | 
					
						
							|  |  |  |         : m_type(type) | 
					
						
							|  |  |  |         , m_value(value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 11:08:46 +02:00
										 |  |  |     static Length make_auto() { return Length(0, Type::Auto); } | 
					
						
							|  |  |  |     static Length make_px(float value) { return Length(value, Type::Px); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     Length resolved(const Length& fallback_for_undefined, const Layout::Node& layout_node, float reference_for_percent) const | 
					
						
							| 
									
										
										
										
											2020-06-24 15:38:21 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (is_undefined()) | 
					
						
							|  |  |  |             return fallback_for_undefined; | 
					
						
							| 
									
										
										
										
											2021-06-12 00:50:16 +02:00
										 |  |  |         if (is_calculated()) | 
					
						
							|  |  |  |             return Length(resolve_calculated_value(layout_node, reference_for_percent), Type::Px); | 
					
						
							| 
									
										
										
										
											2020-06-24 15:38:21 +02:00
										 |  |  |         if (is_percentage()) | 
					
						
							| 
									
										
										
										
											2021-04-15 00:36:14 -07:00
										 |  |  |             return make_px(raw_value() / 100.0f * reference_for_percent); | 
					
						
							| 
									
										
										
										
											2020-06-24 15:38:21 +02:00
										 |  |  |         if (is_relative()) | 
					
						
							|  |  |  |             return make_px(to_px(layout_node)); | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     Length resolved_or_auto(const Layout::Node& layout_node, float reference_for_percent) const | 
					
						
							| 
									
										
										
										
											2020-06-24 15:38:21 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return resolved(make_auto(), layout_node, reference_for_percent); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     Length resolved_or_zero(const Layout::Node& layout_node, float reference_for_percent) const | 
					
						
							| 
									
										
										
										
											2020-06-24 17:45:42 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return resolved(make_px(0), layout_node, reference_for_percent); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 15:08:37 +02:00
										 |  |  |     bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; } | 
					
						
							| 
									
										
										
										
											2020-06-24 11:24:00 +02:00
										 |  |  |     bool is_undefined() const { return m_type == Type::Undefined; } | 
					
						
							| 
									
										
										
										
											2021-06-12 00:03:15 +02:00
										 |  |  |     bool is_percentage() const { return m_type == Type::Percentage || m_type == Type::Calculated; } | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  |     bool is_auto() const { return m_type == Type::Auto; } | 
					
						
							| 
									
										
										
										
											2021-06-12 00:03:15 +02:00
										 |  |  |     bool is_calculated() const { return m_type == Type::Calculated; } | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_absolute() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_type == Type::Cm | 
					
						
							|  |  |  |             || m_type == Type::In | 
					
						
							|  |  |  |             || m_type == Type::Mm | 
					
						
							|  |  |  |             || m_type == Type::Px | 
					
						
							|  |  |  |             || m_type == Type::Pt | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |             || m_type == Type::Pc | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |             || m_type == Type::Q; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-08 20:39:09 +02:00
										 |  |  |     bool is_relative() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_type == Type::Ex | 
					
						
							|  |  |  |             || m_type == Type::Em | 
					
						
							|  |  |  |             || m_type == Type::Rem | 
					
						
							|  |  |  |             || m_type == Type::Vh | 
					
						
							|  |  |  |             || m_type == Type::Vw | 
					
						
							|  |  |  |             || m_type == Type::Vmax | 
					
						
							|  |  |  |             || m_type == Type::Vmin; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |     float raw_value() const { return m_value; } | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     ALWAYS_INLINE float to_px(const Layout::Node& layout_node) const | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (is_relative()) | 
					
						
							|  |  |  |             return relative_length_to_px(layout_node); | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |         constexpr float inch_pixels = 96.0f; | 
					
						
							|  |  |  |         constexpr float centimeter_pixels = (inch_pixels / 2.54f); | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  |         switch (m_type) { | 
					
						
							|  |  |  |         case Type::Auto: | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |         case Type::Cm: | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |             return m_value * centimeter_pixels; // 1cm = 96px/2.54
 | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |         case Type::In: | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |             return m_value * inch_pixels; // 1in = 2.54 cm = 96px
 | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  |         case Type::Px: | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |             return m_value; // 1px = 1/96th of 1in
 | 
					
						
							| 
									
										
										
										
											2020-06-28 15:25:32 +02:00
										 |  |  |         case Type::Pt: | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |             return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
 | 
					
						
							|  |  |  |         case Type::Pc: | 
					
						
							|  |  |  |             return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
 | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |         case Type::Mm: | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |             return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
 | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |         case Type::Q: | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |             return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
 | 
					
						
							| 
									
										
										
										
											2020-06-24 11:24:00 +02:00
										 |  |  |         case Type::Undefined: | 
					
						
							| 
									
										
										
										
											2020-06-24 15:38:21 +02:00
										 |  |  |         case Type::Percentage: | 
					
						
							| 
									
										
										
										
											2021-06-12 00:03:15 +02:00
										 |  |  |         case Type::Calculated: | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 07:55:22 +02:00
										 |  |  |     String to_string() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_auto()) | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |             return "[auto]"; | 
					
						
							| 
									
										
										
										
											2021-01-03 14:05:46 +01:00
										 |  |  |         return String::formatted("[{} {}]", m_value, unit_name()); | 
					
						
							| 
									
										
										
										
											2019-07-24 07:34:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 19:39:33 +01:00
										 |  |  |     bool operator==(const Length& other) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_type == other.m_type && m_value == other.m_value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool operator!=(const Length& other) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return !(*this == other); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 00:03:15 +02:00
										 |  |  |     void set_calculated_style(CalculatedStyleValue* value) { m_calculated_style = value; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     float relative_length_to_px(const Layout::Node&) const; | 
					
						
							| 
									
										
										
										
											2021-06-12 00:50:16 +02:00
										 |  |  |     float resolve_calculated_value(const Layout::Node& layout_node, float reference_for_percent) const; | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |     const char* unit_name() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 11:24:00 +02:00
										 |  |  |     Type m_type { Type::Undefined }; | 
					
						
							| 
									
										
										
										
											2019-11-18 16:25:38 +01:00
										 |  |  |     float m_value { 0 }; | 
					
						
							| 
									
										
										
										
											2021-06-12 00:03:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     CalculatedStyleValue* m_calculated_style { nullptr }; | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-08-18 08:09:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |