| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-02-14 15:07:50 +00:00
										 |  |  |  * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 15:07:50 +00:00
										 |  |  | #include <AK/RefPtr.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-06 19:02:26 +01:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  | #include <LibGfx/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2022-10-24 17:16:08 +01:00
										 |  |  | #include <LibWeb/PixelUnits.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 { | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2021-08-05 16:31:15 +02:00
										 |  |  |         Ch, | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |         Rem, | 
					
						
							| 
									
										
										
										
											2020-09-08 20:39:09 +02:00
										 |  |  |         Vh, | 
					
						
							|  |  |  |         Vw, | 
					
						
							|  |  |  |         Vmax, | 
					
						
							|  |  |  |         Vmin, | 
					
						
							| 
									
										
										
										
											2023-03-17 23:08:45 +01:00
										 |  |  |         Lh, | 
					
						
							|  |  |  |         Rlh, | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |     static Optional<Type> unit_from_name(StringView); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 22:57:35 +02:00
										 |  |  |     // We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
 | 
					
						
							|  |  |  |     // this file already. To break the cyclic dependency, we must move all method definitions out.
 | 
					
						
							|  |  |  |     Length(int value, Type type); | 
					
						
							|  |  |  |     Length(float value, Type type); | 
					
						
							| 
									
										
										
										
											2022-09-13 17:42:39 +02:00
										 |  |  |     ~Length(); | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 22:57:35 +02:00
										 |  |  |     static Length make_auto(); | 
					
						
							| 
									
										
										
										
											2022-10-24 17:16:08 +01:00
										 |  |  |     static Length make_px(CSSPixels value); | 
					
						
							| 
									
										
										
										
											2022-01-25 12:11:23 +00:00
										 |  |  |     static Length make_calculated(NonnullRefPtr<CalculatedStyleValue>); | 
					
						
							| 
									
										
										
										
											2022-01-14 12:23:54 +00:00
										 |  |  |     Length percentage_of(Percentage const&) const; | 
					
						
							| 
									
										
										
										
											2020-06-24 11:08:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 16:17:27 +00:00
										 |  |  |     Length resolved(Layout::Node const& layout_node) const; | 
					
						
							| 
									
										
										
										
											2020-06-24 17:45:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; } | 
					
						
							| 
									
										
										
										
											2022-02-18 20:26:09 +01:00
										 |  |  |     bool is_px() const { return m_type == Type::Px; } | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2021-08-05 16:31:15 +02:00
										 |  |  |             || m_type == Type::Ch | 
					
						
							| 
									
										
										
										
											2020-09-08 20:39:09 +02:00
										 |  |  |             || m_type == Type::Rem | 
					
						
							|  |  |  |             || m_type == Type::Vh | 
					
						
							|  |  |  |             || m_type == Type::Vw | 
					
						
							|  |  |  |             || m_type == Type::Vmax | 
					
						
							| 
									
										
										
										
											2023-03-17 23:08:45 +01:00
										 |  |  |             || m_type == Type::Vmin | 
					
						
							|  |  |  |             || m_type == Type::Lh | 
					
						
							|  |  |  |             || m_type == Type::Rlh; | 
					
						
							| 
									
										
										
										
											2020-09-08 20:39:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |     float raw_value() const { return m_value; } | 
					
						
							| 
									
										
										
										
											2022-07-27 13:45:05 +01:00
										 |  |  |     NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const; | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-08 17:29:52 +00:00
										 |  |  |     CSSPixels to_px(Layout::Node const&) const; | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 23:08:45 +01:00
										 |  |  |     ALWAYS_INLINE CSSPixels to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-05 15:39:40 +01:00
										 |  |  |         if (is_auto()) | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2020-06-23 23:21:58 +02:00
										 |  |  |         if (is_relative()) | 
					
						
							| 
									
										
										
										
											2023-03-17 23:08:45 +01:00
										 |  |  |             return relative_length_to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); | 
					
						
							| 
									
										
										
										
											2022-02-18 16:20:16 +00:00
										 |  |  |         if (is_calculated()) | 
					
						
							|  |  |  |             VERIFY_NOT_REACHED(); // We can't resolve a calculated length from here. :^(
 | 
					
						
							| 
									
										
										
										
											2021-10-05 15:39:40 +01:00
										 |  |  |         return absolute_length_to_px(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-08 17:29:52 +00:00
										 |  |  |     ALWAYS_INLINE CSSPixels absolute_length_to_px() const | 
					
						
							| 
									
										
										
										
											2021-10-05 15:39:40 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											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-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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-06 19:02:26 +01:00
										 |  |  |     ErrorOr<String> to_string() const; | 
					
						
							| 
									
										
										
										
											2019-07-24 07:34:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-13 17:42:39 +02:00
										 |  |  |     // We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
 | 
					
						
							|  |  |  |     // this file already. To break the cyclic dependency, we must move all method definitions out.
 | 
					
						
							|  |  |  |     bool operator==(Length const& other) const; | 
					
						
							| 
									
										
										
										
											2020-12-15 19:39:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 23:08:45 +01:00
										 |  |  |     CSSPixels relative_length_to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const; | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     char const* unit_name() const; | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 16:50:17 +00:00
										 |  |  |     Type m_type; | 
					
						
							| 
									
										
										
										
											2019-11-18 16:25:38 +01:00
										 |  |  |     float m_value { 0 }; | 
					
						
							| 
									
										
										
										
											2021-06-12 00:03:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 11:49:47 +02:00
										 |  |  |     RefPtr<CalculatedStyleValue> m_calculated_style; | 
					
						
							| 
									
										
										
										
											2019-07-01 17:17:32 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-08-18 08:09:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-19 17:30:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-06 19:02:26 +01:00
										 |  |  |         return Formatter<StringView>::format(builder, TRY(length.to_string())); | 
					
						
							| 
									
										
										
										
											2022-01-19 17:30:29 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |