| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  |  * Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2024-10-12 23:30:40 +02:00
										 |  |  |  * Copyright (c) 2024, Glenn Skrzypczak <glenn.skrzypczak@gmail.com> | 
					
						
							| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  | #include <LibWeb/CSS/Resolution.h>
 | 
					
						
							| 
									
										
										
										
											2025-08-18 14:04:57 +01:00
										 |  |  | #include <LibWeb/CSS/Serialize.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  | Resolution::Resolution(double value, ResolutionUnit unit) | 
					
						
							|  |  |  |     : m_unit(unit) | 
					
						
							| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  |     , m_value(value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-30 17:05:23 +00:00
										 |  |  | Resolution Resolution::make_dots_per_pixel(double value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  |     return { value, ResolutionUnit::Dppx }; | 
					
						
							| 
									
										
										
										
											2023-12-30 17:05:23 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  | String Resolution::to_string(SerializationMode serialization_mode) const | 
					
						
							| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-08-18 14:04:57 +01:00
										 |  |  |     // https://drafts.csswg.org/cssom/#serialize-a-css-value
 | 
					
						
							|  |  |  |     // -> <resolution>
 | 
					
						
							|  |  |  |     // The resolution in dots per CSS pixel serialized as per <number> followed by the literal string "dppx".
 | 
					
						
							|  |  |  |     // AD-HOC: WPT expects us to serialize using the actual unit, like for other dimensions.
 | 
					
						
							|  |  |  |     //         https://github.com/w3c/csswg-drafts/issues/12616
 | 
					
						
							|  |  |  |     if (serialization_mode == SerializationMode::ResolvedValue) { | 
					
						
							|  |  |  |         StringBuilder builder; | 
					
						
							|  |  |  |         serialize_a_number(builder, to_dots_per_pixel()); | 
					
						
							|  |  |  |         builder.append("dppx"sv); | 
					
						
							|  |  |  |         return builder.to_string_without_validation(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |     serialize_a_number(builder, raw_value()); | 
					
						
							|  |  |  |     builder.append(unit_name()); | 
					
						
							|  |  |  |     return builder.to_string_without_validation(); | 
					
						
							| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-20 13:02:41 +01:00
										 |  |  | double Resolution::to_dots_per_pixel() const | 
					
						
							| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-04 12:27:20 +01:00
										 |  |  |     return ratio_between_units(m_unit, ResolutionUnit::Dppx) * m_value; | 
					
						
							| 
									
										
										
										
											2022-02-21 17:51:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |