| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  |  * Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2021-08-09 21:28:56 +02:00
										 |  |  |  * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-03-30 15:46:05 +01:00
										 |  |  |  * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 00:50:16 +02:00
										 |  |  | #include <AK/NonnullOwnPtr.h>
 | 
					
						
							|  |  |  | #include <AK/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-09 09:28:38 +02:00
										 |  |  | #include <LibGfx/Font/Font.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  | #include <LibGfx/Rect.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | #include <LibWeb/CSS/Length.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-14 12:23:54 +00:00
										 |  |  | #include <LibWeb/CSS/Percentage.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 15:01:28 +01:00
										 |  |  | #include <LibWeb/HTML/BrowsingContext.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:08:16 +02:00
										 |  |  | #include <LibWeb/HTML/HTMLHtmlElement.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-08 07:51:03 +02:00
										 |  |  | #include <LibWeb/Layout/Node.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 20:01:35 +02:00
										 |  |  | namespace Web::CSS { | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 12:39:40 +01:00
										 |  |  | Length::FontMetrics::FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const& pixel_metrics) | 
					
						
							| 
									
										
										
										
											2023-04-28 16:29:12 +01:00
										 |  |  |     : font_size(font_size) | 
					
						
							|  |  |  |     , x_height(pixel_metrics.x_height) | 
					
						
							| 
									
										
										
										
											2023-04-28 17:10:30 +01:00
										 |  |  |     // FIXME: This is only approximately the cap height. The spec suggests measuring the "O" glyph:
 | 
					
						
							|  |  |  |     //        https://www.w3.org/TR/css-values-4/#cap
 | 
					
						
							|  |  |  |     , cap_height(pixel_metrics.ascent) | 
					
						
							| 
									
										
										
										
											2024-06-04 07:02:22 +02:00
										 |  |  |     , zero_advance(pixel_metrics.advance_of_ascii_zero) | 
					
						
							| 
									
										
										
										
											2024-01-12 12:39:40 +01:00
										 |  |  |     , line_height(round(pixel_metrics.line_spacing())) | 
					
						
							| 
									
										
										
										
											2023-04-28 16:29:12 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-24 10:50:57 +02:00
										 |  |  | Length::Length(double value, Type type) | 
					
						
							| 
									
										
										
										
											2021-09-30 22:57:35 +02:00
										 |  |  |     : m_type(type) | 
					
						
							|  |  |  |     , m_value(value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-09-13 17:42:39 +02:00
										 |  |  | Length::~Length() = default; | 
					
						
							| 
									
										
										
										
											2021-09-30 22:57:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | Length Length::make_auto() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Length(0, Type::Auto); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-14 12:23:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-24 17:16:08 +01:00
										 |  |  | Length Length::make_px(CSSPixels value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-12 21:37:35 +03:00
										 |  |  |     return Length(value.to_double(), Type::Px); | 
					
						
							| 
									
										
										
										
											2022-10-24 17:16:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 12:23:54 +00:00
										 |  |  | Length Length::percentage_of(Percentage const& percentage) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-18 16:50:17 +00:00
										 |  |  |     if (is_auto()) { | 
					
						
							|  |  |  |         dbgln("Attempting to get percentage of an auto length, this seems wrong? But for now we just return the original length."); | 
					
						
							| 
									
										
										
										
											2022-01-14 12:23:54 +00:00
										 |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-11 06:20:48 +00:00
										 |  |  |     return Length { percentage.as_fraction() * raw_value(), m_type }; | 
					
						
							| 
									
										
										
										
											2022-01-14 12:23:54 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:07:41 +01:00
										 |  |  | CSSPixels Length::font_relative_length_to_px(Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Em: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * font_metrics.font_size.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Rem: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * root_font_metrics.font_size.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Ex: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * font_metrics.x_height.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-28 16:58:12 +01:00
										 |  |  |     case Type::Rex: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * root_font_metrics.x_height.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-28 17:10:30 +01:00
										 |  |  |     case Type::Cap: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * font_metrics.cap_height.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-28 17:10:30 +01:00
										 |  |  |     case Type::Rcap: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * root_font_metrics.cap_height.to_double()); | 
					
						
							| 
									
										
										
										
											2021-08-05 16:31:15 +02:00
										 |  |  |     case Type::Ch: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * font_metrics.zero_advance.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-28 16:58:12 +01:00
										 |  |  |     case Type::Rch: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * root_font_metrics.zero_advance.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-28 17:18:18 +01:00
										 |  |  |     case Type::Ic: | 
					
						
							|  |  |  |         // FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph"
 | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * font_metrics.font_size.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-28 17:18:18 +01:00
										 |  |  |     case Type::Ric: | 
					
						
							|  |  |  |         // FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph"
 | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * root_font_metrics.font_size.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Lh: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * font_metrics.line_height.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Rlh: | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |         return CSSPixels::nearest_value_for(m_value * root_font_metrics.line_height.to_double()); | 
					
						
							| 
									
										
										
										
											2023-04-28 20:07:41 +01:00
										 |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSSPixels Length::viewport_relative_length_to_px(CSSPixelRect const& viewport_rect) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							| 
									
										
										
										
											2020-09-08 20:39:09 +02:00
										 |  |  |     case Type::Vw: | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svw: | 
					
						
							|  |  |  |     case Type::Lvw: | 
					
						
							|  |  |  |     case Type::Dvw: | 
					
						
							| 
									
										
										
										
											2023-08-31 19:57:20 -05:00
										 |  |  |         return viewport_rect.width() * (CSSPixels::nearest_value_for(m_value) / 100); | 
					
						
							| 
									
										
										
										
											2020-09-08 20:39:09 +02:00
										 |  |  |     case Type::Vh: | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svh: | 
					
						
							|  |  |  |     case Type::Lvh: | 
					
						
							|  |  |  |     case Type::Dvh: | 
					
						
							| 
									
										
										
										
											2023-08-31 19:57:20 -05:00
										 |  |  |         return viewport_rect.height() * (CSSPixels::nearest_value_for(m_value) / 100); | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Vi: | 
					
						
							|  |  |  |     case Type::Svi: | 
					
						
							|  |  |  |     case Type::Lvi: | 
					
						
							|  |  |  |     case Type::Dvi: | 
					
						
							|  |  |  |         // FIXME: Select the width or height based on which is the inline axis.
 | 
					
						
							| 
									
										
										
										
											2023-08-31 19:57:20 -05:00
										 |  |  |         return viewport_rect.width() * (CSSPixels::nearest_value_for(m_value) / 100); | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Vb: | 
					
						
							|  |  |  |     case Type::Svb: | 
					
						
							|  |  |  |     case Type::Lvb: | 
					
						
							|  |  |  |     case Type::Dvb: | 
					
						
							|  |  |  |         // FIXME: Select the width or height based on which is the block axis.
 | 
					
						
							| 
									
										
										
										
											2023-08-31 19:57:20 -05:00
										 |  |  |         return viewport_rect.height() * (CSSPixels::nearest_value_for(m_value) / 100); | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  |     case Type::Vmin: | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svmin: | 
					
						
							|  |  |  |     case Type::Lvmin: | 
					
						
							|  |  |  |     case Type::Dvmin: | 
					
						
							| 
									
										
										
										
											2023-08-31 19:57:20 -05:00
										 |  |  |         return min(viewport_rect.width(), viewport_rect.height()) * (CSSPixels::nearest_value_for(m_value) / 100); | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  |     case Type::Vmax: | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svmax: | 
					
						
							|  |  |  |     case Type::Lvmax: | 
					
						
							|  |  |  |     case Type::Dvmax: | 
					
						
							| 
									
										
										
										
											2023-08-31 19:57:20 -05:00
										 |  |  |         return max(viewport_rect.width(), viewport_rect.height()) * (CSSPixels::nearest_value_for(m_value) / 100); | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  | Length::ResolutionContext Length::ResolutionContext::for_window(HTML::Window const& window) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const& initial_font = window.associated_document().style_computer().initial_font(); | 
					
						
							|  |  |  |     Gfx::FontPixelMetrics const& initial_font_metrics = initial_font.pixel_metrics(); | 
					
						
							|  |  |  |     Length::FontMetrics font_metrics { CSSPixels { initial_font.pixel_size() }, initial_font_metrics }; | 
					
						
							|  |  |  |     return Length::ResolutionContext { | 
					
						
							|  |  |  |         .viewport_rect = window.page().web_exposed_screen_area(), | 
					
						
							|  |  |  |         .font_metrics = font_metrics, | 
					
						
							|  |  |  |         .root_font_metrics = font_metrics, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 12:39:39 +02:00
										 |  |  | Length::ResolutionContext Length::ResolutionContext::for_layout_node(Layout::Node const& node) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const* root_element = node.document().document_element(); | 
					
						
							|  |  |  |     VERIFY(root_element); | 
					
						
							|  |  |  |     VERIFY(root_element->layout_node()); | 
					
						
							|  |  |  |     return Length::ResolutionContext { | 
					
						
							| 
									
										
										
										
											2023-08-22 16:00:42 +02:00
										 |  |  |         .viewport_rect = node.navigable()->viewport_rect(), | 
					
						
							| 
									
										
										
										
											2024-01-12 12:39:40 +01:00
										 |  |  |         .font_metrics = { node.computed_values().font_size(), node.first_available_font().pixel_metrics() }, | 
					
						
							|  |  |  |         .root_font_metrics = { root_element->layout_node()->computed_values().font_size(), root_element->layout_node()->first_available_font().pixel_metrics() }, | 
					
						
							| 
									
										
										
										
											2023-06-02 12:39:39 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSSPixels Length::to_px(ResolutionContext const& context) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return to_px(context.viewport_rect, context.font_metrics, context.root_font_metrics); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-02 11:17:06 +01:00
										 |  |  | CSSPixels Length::to_px_slow_case(Layout::Node const& layout_node) const | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-28 20:07:41 +01:00
										 |  |  |     if (is_auto()) { | 
					
						
							|  |  |  |         // FIXME: We really, really shouldn't end up here, but we do, and so frequently that
 | 
					
						
							|  |  |  |         //        adding a dbgln() here outputs a couple hundred lines loading `welcome.html`.
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  |     if (!layout_node.document().browsing_context()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2023-04-28 16:29:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:07:41 +01:00
										 |  |  |     if (is_font_relative()) { | 
					
						
							|  |  |  |         auto* root_element = layout_node.document().document_element(); | 
					
						
							|  |  |  |         if (!root_element || !root_element->layout_node()) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         FontMetrics font_metrics { | 
					
						
							| 
									
										
										
										
											2023-08-31 19:53:41 -05:00
										 |  |  |             layout_node.computed_values().font_size(), | 
					
						
							| 
									
										
										
										
											2023-12-09 23:42:02 +01:00
										 |  |  |             layout_node.first_available_font().pixel_metrics(), | 
					
						
							| 
									
										
										
										
											2023-04-28 20:07:41 +01:00
										 |  |  |         }; | 
					
						
							|  |  |  |         FontMetrics root_font_metrics { | 
					
						
							| 
									
										
										
										
											2023-08-31 19:53:41 -05:00
										 |  |  |             root_element->layout_node()->computed_values().font_size(), | 
					
						
							| 
									
										
										
										
											2023-12-09 23:42:02 +01:00
										 |  |  |             root_element->layout_node()->first_available_font().pixel_metrics(), | 
					
						
							| 
									
										
										
										
											2023-04-28 20:07:41 +01:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return font_relative_length_to_px(font_metrics, root_font_metrics); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     VERIFY(is_viewport_relative()); | 
					
						
							| 
									
										
										
										
											2023-08-22 16:00:42 +02:00
										 |  |  |     auto const& viewport_rect = layout_node.document().viewport_rect(); | 
					
						
							| 
									
										
										
										
											2023-04-28 20:07:41 +01:00
										 |  |  |     return viewport_relative_length_to_px(viewport_rect); | 
					
						
							| 
									
										
										
										
											2021-09-23 13:13:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  | String Length::to_string() const | 
					
						
							| 
									
										
										
										
											2022-02-21 17:35:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (is_auto()) | 
					
						
							| 
									
										
										
										
											2023-02-25 16:40:37 +01:00
										 |  |  |         return "auto"_string; | 
					
						
							| 
									
										
										
										
											2024-03-03 09:18:03 -07:00
										 |  |  |     return MUST(String::formatted("{:.5}{}", m_value, unit_name())); | 
					
						
							| 
									
										
										
										
											2022-02-21 17:35:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  | StringView Length::unit_name() const | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Em: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "em"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Rem: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "rem"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Ex: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "ex"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 16:58:12 +01:00
										 |  |  |     case Type::Rex: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "rex"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:10:30 +01:00
										 |  |  |     case Type::Cap: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "cap"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:10:30 +01:00
										 |  |  |     case Type::Rcap: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "rcap"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Ch: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "ch"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 16:58:12 +01:00
										 |  |  |     case Type::Rch: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "rch"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:18:18 +01:00
										 |  |  |     case Type::Ic: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "ic"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:18:18 +01:00
										 |  |  |     case Type::Ric: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "ric"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Lh: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "lh"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Rlh: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "rlh"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Vw: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "vw"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svw: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "svw"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Lvw: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "lvw"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Dvw: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "dvw"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Vh: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "vh"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svh: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "svh"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Lvh: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "lvh"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Dvh: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "dvh"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Vi: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "vi"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Svi: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "svi"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Lvi: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "lvi"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Dvi: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "dvi"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Vb: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "vb"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Svb: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "svb"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Lvb: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "lvb"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     case Type::Dvb: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "dvb"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Vmin: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "vmin"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svmin: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "svmin"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Lvmin: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "lvmin"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Dvmin: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "dvmin"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Vmax: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "vmax"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Svmax: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "svmax"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Lvmax: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "lvmax"sv; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     case Type::Dvmax: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "dvmax"sv; | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |     case Type::Cm: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "cm"sv; | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |     case Type::Mm: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "mm"sv; | 
					
						
							| 
									
										
										
										
											2020-09-29 19:06:58 +01:00
										 |  |  |     case Type::Q: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "Q"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::In: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "in"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Pt: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "pt"sv; | 
					
						
							| 
									
										
										
										
											2020-10-05 16:18:07 +01:00
										 |  |  |     case Type::Pc: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "pc"sv; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     case Type::Px: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "px"sv; | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |     case Type::Auto: | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |         return "auto"sv; | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  | Optional<Length::Type> Length::unit_from_name(StringView name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     if (name.equals_ignoring_ascii_case("em"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Em; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("rem"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Rem; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("ex"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Ex; | 
					
						
							| 
									
										
										
										
											2023-04-28 16:58:12 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("rex"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Rex; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:10:30 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("cap"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Cap; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("rcap"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Rcap; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("ch"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Ch; | 
					
						
							| 
									
										
										
										
											2023-04-28 16:58:12 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("rch"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Rch; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:18:18 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("ic"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Ic; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("ric"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Ric; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("lh"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Lh; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("rlh"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Rlh; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("vw"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Vw; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("svw"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Svw; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("lvw"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Lvw; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("dvw"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Dvw; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("vh"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Vh; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("svh"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Svh; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("lvh"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Lvh; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("dvh"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Dvh; | 
					
						
							| 
									
										
										
										
											2023-04-28 19:50:09 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("vi"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Vi; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("svi"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Svi; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("lvi"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Lvi; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("dvi"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Dvi; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("vb"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Vb; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("svb"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Svb; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("lvb"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Lvb; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("dvb"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Dvb; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("vmin"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Vmin; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("svmin"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Svmin; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("lvmin"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Lvmin; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("dvmin"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Dvmin; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("vmax"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Vmax; | 
					
						
							| 
									
										
										
										
											2023-04-28 17:46:49 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("svmax"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Svmax; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("lvmax"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Lvmax; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("dvmax"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Dvmax; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("cm"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Cm; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("mm"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Mm; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("Q"sv)) { | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |         return Length::Type::Q; | 
					
						
							| 
									
										
										
										
											2023-04-24 14:34:44 +01:00
										 |  |  |     } else if (name.equals_ignoring_ascii_case("in"sv)) { | 
					
						
							|  |  |  |         return Length::Type::In; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("pt"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Pt; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("pc"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Pc; | 
					
						
							|  |  |  |     } else if (name.equals_ignoring_ascii_case("px"sv)) { | 
					
						
							|  |  |  |         return Length::Type::Px; | 
					
						
							| 
									
										
										
										
											2022-02-22 12:10:48 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 16:29:12 +01:00
										 |  |  | Optional<Length> Length::absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const | 
					
						
							| 
									
										
										
										
											2023-03-30 15:01:23 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (is_px()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (is_absolute() || is_relative()) { | 
					
						
							| 
									
										
										
										
											2023-04-28 16:29:12 +01:00
										 |  |  |         auto px = to_px(viewport_rect, font_metrics, root_font_metrics); | 
					
						
							| 
									
										
										
										
											2023-03-30 15:01:23 +01:00
										 |  |  |         return CSS::Length::make_px(px); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 16:29:12 +01:00
										 |  |  | Length Length::absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const | 
					
						
							| 
									
										
										
										
											2023-03-30 15:01:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-28 16:29:12 +01:00
										 |  |  |     return absolutize(viewport_rect, font_metrics, root_font_metrics).value_or(*this); | 
					
						
							| 
									
										
										
										
											2023-03-30 15:01:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 15:05:56 +00:00
										 |  |  | Length Length::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const& layout_node, Length const& reference_value) | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return calculated->resolve_length_percentage(layout_node, reference_value).value(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 15:05:56 +00:00
										 |  |  | Length Length::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const& layout_node, CSSPixels reference_value) | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return calculated->resolve_length_percentage(layout_node, reference_value).value(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 17:55:46 +02:00
										 |  |  | } |