Truly independent web browser
LibWeb: Move the default sizing algorithm into layout
Aliaksandr Kalenik856933f2a34b
diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rsindex 6b2bb7bb98b..1dd95015c53 100644--- a/Libraries/LibWeb/Rust/src/layout/mod.rs+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs@@ -52,6 +52,7 @@ mod partial_relayout;pub(crate) mod prepared_inline_content;pub(crate) mod quote_depth;pub(crate) mod rendered_text;+pub(crate) mod replaced_sizing;mod replaced_with_children_formatting_context;pub(crate) mod row_reads;pub(crate) mod run_repeatability;diff --git a/Libraries/LibWeb/Rust/src/layout/replaced_sizing.rs b/Libraries/LibWeb/Rust/src/layout/replaced_sizing.rsnew file mode 100644index 00000000000..ad6a66b5406--- /dev/null+++ b/Libraries/LibWeb/Rust/src/layout/replaced_sizing.rs@@ -0,0 +1,134 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++use crate::css::css_pixels::{CssPixelSize, CssPixels};++#[derive(Clone, Copy, Debug, PartialEq)]+pub(crate) struct Fraction {+ pub numerator: CssPixels,+ pub denominator: CssPixels,+}++impl Fraction {+ pub(crate) fn one() -> Self {+ Self {+ numerator: CssPixels::from_integer(1),+ denominator: CssPixels::from_integer(1),+ }+ }++ pub(crate) fn of(numerator: CssPixels, denominator: CssPixels) -> Self {+ Self { numerator, denominator }+ }++ fn to_css_pixels(self) -> CssPixels {+ self.numerator.div_as_fraction(self.denominator)+ }++ fn might_be_saturated(self) -> bool {+ let raw = self.to_css_pixels().raw_value();+ raw == i32::MAX || raw == i32::MIN+ }++ fn to_double(self) -> f64 {+ self.numerator.to_double() / self.denominator.to_double()+ }++ pub(crate) fn multiply(self, value: CssPixels) -> CssPixels {+ let wide = value.raw_value() as i64 * self.numerator.raw_value() as i64;+ CssPixels::from_raw((wide / self.denominator.raw_value() as i64).clamp(i32::MIN as i64, i32::MAX as i64) as i32)+ }++ fn divide(self, value: CssPixels) -> CssPixels {+ let wide = value.raw_value() as i64 * self.denominator.raw_value() as i64;+ CssPixels::from_raw((wide / self.numerator.raw_value() as i64).clamp(i32::MIN as i64, i32::MAX as i64) as i32)+ }++ pub(crate) fn ge(self, other: Self) -> bool {+ let left = self.numerator.raw_value() as i64 * other.denominator.raw_value() as i64;+ let right = other.numerator.raw_value() as i64 * self.denominator.raw_value() as i64;+ left >= right+ }+}++#[derive(Clone, Copy, Debug, Default, PartialEq)]+pub(crate) struct SizeWithAspectRatio {+ pub width: Option<CssPixels>,+ pub height: Option<CssPixels>,+ pub aspect_ratio: Option<Fraction>,+}++impl SizeWithAspectRatio {+ pub(crate) fn from_ffi(natural: &crate::painting::host::FfiNaturalSize) -> Self {+ Self {+ width: natural.width.has_value.then_some(natural.width.value),+ height: natural.height.has_value.then_some(natural.height.value),+ aspect_ratio: natural.has_aspect_ratio.then_some(Fraction::of(+ natural.aspect_ratio_numerator,+ natural.aspect_ratio_denominator,+ )),+ }+ }+}++pub(crate) fn run_default_sizing_algorithm(+ specified_width: Option<CssPixels>,+ specified_height: Option<CssPixels>,+ natural: &SizeWithAspectRatio,+ default_size: CssPixelSize,+) -> CssPixelSize {+ if let (Some(width), Some(height)) = (specified_width, specified_height) {+ return CssPixelSize::new(width, height);+ }+ if specified_width.is_some() || specified_height.is_some() {+ if let Some(aspect_ratio) = natural.aspect_ratio+ && !aspect_ratio.might_be_saturated()+ {+ if let Some(width) = specified_width {+ return CssPixelSize::new(width, aspect_ratio.divide(width));+ }+ if let Some(height) = specified_height {+ return CssPixelSize::new(aspect_ratio.multiply(height), height);+ }+ }+ if let (Some(height), Some(natural_width)) = (specified_height, natural.width) {+ return CssPixelSize::new(natural_width, height);+ }+ if let (Some(width), Some(natural_height)) = (specified_width, natural.height) {+ return CssPixelSize::new(width, natural_height);+ }+ if let Some(height) = specified_height {+ return CssPixelSize::new(default_size.width, height);+ }+ if let Some(width) = specified_width {+ return CssPixelSize::new(width, default_size.height);+ }+ unreachable!();+ }+ if natural.width.is_some() || natural.height.is_some() {+ return run_default_sizing_algorithm(natural.width, natural.height, natural, default_size);+ }+ if let Some(aspect_ratio) = natural.aspect_ratio+ && !aspect_ratio.might_be_saturated()+ {+ if default_size.is_empty() {+ return default_size;+ }+ let default_width = default_size.width.to_double();+ let default_height = default_size.height.to_double();+ if aspect_ratio.to_double() >= default_width / default_height {+ return CssPixelSize::new(+ default_size.width,+ CssPixels::nearest_value_for(default_width / aspect_ratio.to_double()),+ );+ }+ return CssPixelSize::new(+ CssPixels::nearest_value_for(default_height * aspect_ratio.to_double()),+ default_size.height,+ );+ }+ default_size+}diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex a41ff0e2cea..9677183f1ed 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -2354,7 +2354,7 @@ impl<'pass> SvgFormattingContext<'pass> {// does not have an intrinsic size (such as image types with no defined dimensions), it is// assumed to have a width of 300px and a height of 150px.fn svg_image_bounding_box(&self, image_box: Node) -> SvgCssPixelRect {- use crate::painting::record::paint::replaced::{Fraction, SizeWithAspectRatio, run_default_sizing_algorithm};+ use crate::layout::replaced_sizing::{Fraction, SizeWithAspectRatio, run_default_sizing_algorithm};let style = self.style(image_box);let specified_width = style.width()diff --git a/Libraries/LibWeb/Rust/src/painting/layer_image_paint_facts.rs b/Libraries/LibWeb/Rust/src/painting/layer_image_paint_facts.rsindex 7e1b3a87e11..a88ae6ebffa 100644--- a/Libraries/LibWeb/Rust/src/painting/layer_image_paint_facts.rs+++ b/Libraries/LibWeb/Rust/src/painting/layer_image_paint_facts.rs@@ -4,9 +4,9 @@* SPDX-License-Identifier: BSD-2-Clause*/+use crate::layout::replaced_sizing::SizeWithAspectRatio;use crate::painting::host::{FfiLayerImageList, FfiLayerImagePaintFacts};use crate::painting::image_content::ImageContent;-use crate::painting::record::paint::replaced::SizeWithAspectRatio;use libgfx_rust::Color;#[derive(Clone, Debug, Default, PartialEq)]diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rsindex 72665183979..843e10d6b2b 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs@@ -13,6 +13,7 @@ use crate::css::css_pixels::CssPixels;use crate::css::css_pixels::{CssPixelRect, CssPixelSize};use crate::css::style_value::StyleValueData;use crate::layout::node_data::{NodeFlag, NodeSlotId};+use crate::layout::replaced_sizing::{SizeWithAspectRatio, run_default_sizing_algorithm};use crate::painting::border_radii::BorderRadii;use crate::painting::host::{FfiLayerImageList, RootBackgroundSource};use crate::painting::paint_read::{GeometryRead, PaintRead};@@ -23,7 +24,6 @@ use crate::painting::paintable_geometry::{};use crate::painting::record::PaintRecorder;use crate::painting::record::paint::background::{BackgroundBox, background_box_for};-use crate::painting::record::paint::replaced::{SizeWithAspectRatio, run_default_sizing_algorithm};use crate::painting::style_queries;use crate::painting::visual_context::node_values::{border_radii_data, mix_blend_mode_to_compositing_and_blending_operator,diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rsindex 71e109847a8..9dae83a1355 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rs@@ -11,6 +11,7 @@ use crate::css::css_enums::{image_rendering, object_fit};use crate::css::css_pixels::CssPixels;use crate::css::css_pixels::{CssPixelRect, CssPixelSize};use crate::layout::node_data::NodeSlotId;+use crate::layout::replaced_sizing::{Fraction, run_default_sizing_algorithm};use crate::painting::display_list::builder::PendingInlineClip;use crate::painting::display_list::commands::{CanvasId, CompositorContextId, CompositorNestedContextWheelRegion};use crate::painting::force_dark::ForceDarkRole;@@ -39,133 +40,6 @@ fn replaced_content_clip_geometry<O: Observer>((content_rect, corner_radii)}-#[derive(Clone, Copy, Debug, PartialEq)]-pub(crate) struct Fraction {- pub numerator: CssPixels,- pub denominator: CssPixels,-}--impl Fraction {- fn one() -> Self {- Self {- numerator: CssPixels::from_integer(1),- denominator: CssPixels::from_integer(1),- }- }-- pub(crate) fn of(numerator: CssPixels, denominator: CssPixels) -> Self {- Self { numerator, denominator }- }-- fn to_css_pixels(self) -> CssPixels {- self.numerator.div_as_fraction(self.denominator)- }-- fn might_be_saturated(self) -> bool {- let raw = self.to_css_pixels().raw_value();- raw == i32::MAX || raw == i32::MIN- }-- fn to_double(self) -> f64 {- self.numerator.to_double() / self.denominator.to_double()- }-- fn multiply(self, value: CssPixels) -> CssPixels {- let wide = value.raw_value() as i64 * self.numerator.raw_value() as i64;- CssPixels::from_raw((wide / self.denominator.raw_value() as i64).clamp(i32::MIN as i64, i32::MAX as i64) as i32)- }-- fn divide(self, value: CssPixels) -> CssPixels {- let wide = value.raw_value() as i64 * self.denominator.raw_value() as i64;- CssPixels::from_raw((wide / self.numerator.raw_value() as i64).clamp(i32::MIN as i64, i32::MAX as i64) as i32)- }-- fn ge(self, other: Self) -> bool {- let left = self.numerator.raw_value() as i64 * other.denominator.raw_value() as i64;- let right = other.numerator.raw_value() as i64 * self.denominator.raw_value() as i64;- left >= right- }-}--#[derive(Clone, Copy, Debug, Default, PartialEq)]-pub(crate) struct SizeWithAspectRatio {- pub width: Option<CssPixels>,- pub height: Option<CssPixels>,- pub aspect_ratio: Option<Fraction>,-}--impl SizeWithAspectRatio {- pub(crate) fn from_ffi(natural: &crate::painting::host::FfiNaturalSize) -> Self {- Self {- width: natural.width.has_value.then_some(natural.width.value),- height: natural.height.has_value.then_some(natural.height.value),- aspect_ratio: natural.has_aspect_ratio.then_some(Fraction::of(- natural.aspect_ratio_numerator,- natural.aspect_ratio_denominator,- )),- }- }-}--pub(crate) fn run_default_sizing_algorithm(- specified_width: Option<CssPixels>,- specified_height: Option<CssPixels>,- natural: &SizeWithAspectRatio,- default_size: CssPixelSize,-) -> CssPixelSize {- if let (Some(width), Some(height)) = (specified_width, specified_height) {- return CssPixelSize::new(width, height);- }- if specified_width.is_some() || specified_height.is_some() {- if let Some(aspect_ratio) = natural.aspect_ratio- && !aspect_ratio.might_be_saturated()- {- if let Some(width) = specified_width {- return CssPixelSize::new(width, aspect_ratio.divide(width));- }- if let Some(height) = specified_height {- return CssPixelSize::new(aspect_ratio.multiply(height), height);- }- }- if let (Some(height), Some(natural_width)) = (specified_height, natural.width) {- return CssPixelSize::new(natural_width, height);- }- if let (Some(width), Some(natural_height)) = (specified_width, natural.height) {- return CssPixelSize::new(width, natural_height);- }- if let Some(height) = specified_height {- return CssPixelSize::new(default_size.width, height);- }- if let Some(width) = specified_width {- return CssPixelSize::new(width, default_size.height);- }- unreachable!();- }- if natural.width.is_some() || natural.height.is_some() {- return run_default_sizing_algorithm(natural.width, natural.height, natural, default_size);- }- if let Some(aspect_ratio) = natural.aspect_ratio- && !aspect_ratio.might_be_saturated()- {- if default_size.is_empty() {- return default_size;- }- let default_width = default_size.width.to_double();- let default_height = default_size.height.to_double();- if aspect_ratio.to_double() >= default_width / default_height {- return CssPixelSize::new(- default_size.width,- CssPixels::nearest_value_for(default_width / aspect_ratio.to_double()),- );- }- return CssPixelSize::new(- CssPixels::nearest_value_for(default_height * aspect_ratio.to_double()),- default_size.height,- );- }- default_size-}-pub(crate) fn get_replaced_box_painting_area<O: Observer>(recorder: &mut PaintRecorder<'_, O>,paintable: NodeSlotId,diff --git a/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rs b/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rsindex e39a188743c..805ade0b25d 100644--- a/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rs+++ b/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rs@@ -4,13 +4,13 @@* SPDX-License-Identifier: BSD-2-Clause*/+use crate::layout::replaced_sizing::SizeWithAspectRatio;use crate::painting::host::{FfiCanvasPaintFacts, FfiFormControlPaintFacts, FfiNavigableContainerPaintFacts, FfiReplacedImagePaintFacts,FfiVideoPaintFacts, FfiVideoRepresentation,};use crate::painting::image_content::ImageContent;use crate::painting::record::damage::PaintDamage;-use crate::painting::record::paint::replaced::SizeWithAspectRatio;use libgfx_rust::image_frame::ImageFrameHandle;#[derive(Clone, Copy, Debug, PartialEq, Eq)]