1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 14:27:34 +00:00

LibWeb: Use fixed-point saturated arithmetics for CSSPixels

Using fixed-point saturated arithmetics for CSSPixels allows to avoid
accumulating floating-point errors.

This implementation is not complete yet: currently saturated
arithmetics implemented only for addition. But it is enough to not
regress any of layout tests we have :)

See https://github.com/SerenityOS/serenity/issues/18566
This commit is contained in:
Aliaksandr Kalenik 2023-07-23 01:09:39 +02:00 committed by Andreas Kling
parent 5cdd03fc53
commit bec07d4af7
136 changed files with 1938 additions and 1844 deletions

View file

@ -987,7 +987,7 @@ void FlexFormattingContext::resolve_flexible_lengths_for_line(FlexLine& line)
// AD-HOC: We allow the remaining free space to be infinite, but we can't let infinity
// leak into the layout geometry, so we treat infinity as zero when used in arithmetic.
auto remaining_free_space_or_zero_if_infinite = isfinite(line.remaining_free_space.to_double()) ? line.remaining_free_space : 0;
auto remaining_free_space_or_zero_if_infinite = !line.remaining_free_space.might_be_saturated() ? line.remaining_free_space : 0;
// c. If the remaining free space is non-zero, distribute it proportional to the flex factors:
if (line.remaining_free_space != 0) {
@ -1096,7 +1096,7 @@ void FlexFormattingContext::resolve_flexible_lengths_for_line(FlexLine& line)
// AD-HOC: Due to the way we calculate the remaining free space, it can be infinite when sizing
// under a max-content constraint. In that case, we can simply set it to zero here.
if (!isfinite(line.remaining_free_space.to_double()))
if (line.remaining_free_space.might_be_saturated())
line.remaining_free_space = 0;
// 6. Set each items used main size to its target main size.

View file

@ -803,7 +803,7 @@ void GridFormattingContext::distribute_extra_space_across_spanned_tracks_base_si
// 2. Distribute space up to limits:
// FIXME: If a fixed-point type were used to represent CSS pixels, it would be possible to compare with 0
// instead of epsilon.
while (extra_space > NumericLimits<double>().epsilon()) {
while (extra_space > CSSPixels::epsilon()) {
auto all_frozen = all_of(affected_tracks, [](auto const& track) { return track.base_size_frozen; });
if (all_frozen)
break;
@ -893,7 +893,7 @@ void GridFormattingContext::distribute_extra_space_across_spanned_tracks_growth_
// 2. Distribute space up to limits:
// FIXME: If a fixed-point type were used to represent CSS pixels, it would be possible to compare with 0
// instead of epsilon.
while (extra_space > NumericLimits<double>().epsilon()) {
while (extra_space > CSSPixels::epsilon()) {
auto all_frozen = all_of(affected_tracks, [](auto const& track) { return track.growth_limit_frozen; });
if (all_frozen)
break;

View file

@ -173,7 +173,7 @@ void SVGFormattingContext::run(Box const& box, LayoutMode layout_mode, Available
// The initial value for preserveAspectRatio is xMidYMid meet.
auto preserve_aspect_ratio = svg_svg_element.preserve_aspect_ratio().value_or(SVG::PreserveAspectRatio {});
auto viewbox_transform = scale_and_align_viewbox_content(preserve_aspect_ratio, view_box, { scale_width.to_double(), scale_height.to_double() }, svg_box_state);
auto viewbox_transform = scale_and_align_viewbox_content(preserve_aspect_ratio, view_box, { scale_width, scale_height }, svg_box_state);
path_transform = Gfx::AffineTransform {}.translate(viewbox_transform.offset.to_type<double>().to_type<float>()).scale(viewbox_transform.scale_factor, viewbox_transform.scale_factor).translate({ -view_box.min_x, -view_box.min_y }).multiply(path_transform);
viewbox_scale = viewbox_transform.scale_factor;
}