Skip to content

Truly independent web browser

LibWeb: Place repeated background tiles exactly

Andreas Klingdba450d4145e
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
index dfc08edfbf3..09456433df1 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
@@ -628,6 +628,7 @@ fn paint_image_layer_in_current_space<O: Observer>(
image_rect.x = background_positioning_area.left() + layer.position_x;
image_rect.y = background_positioning_area.top() + layer.position_y;
+ let anchor_tile_rect = image_rect;
// Repetition
let repeat_x;
@@ -662,9 +663,8 @@ fn paint_image_layer_in_current_space<O: Observer>(
_ => repeat_x = false,
}
// Move image_rect to the left-most tile position that is still visible
- if repeat_x && image_rect.x > css_clip_rect.x {
- let x_delta = floor_css(x_step * ceil_fraction(image_rect.x - css_clip_rect.x, x_step));
- image_rect.x -= x_delta;
+ if repeat_x {
+ image_rect.x = first_css_tile_start(image_rect.x, css_clip_rect.x, x_step);
}
match layer.repeat_y {
@@ -691,9 +691,8 @@ fn paint_image_layer_in_current_space<O: Observer>(
_ => repeat_y = false,
}
// Move image_rect to the top-most tile position that is still visible
- if repeat_y && image_rect.y > css_clip_rect.y {
- let y_delta = floor_css(y_step * ceil_fraction(image_rect.y - css_clip_rect.y, y_step));
- image_rect.y -= y_delta;
+ if repeat_y {
+ image_rect.y = first_css_tile_start(image_rect.y, css_clip_rect.y, y_step);
}
let initial_image_x = image_rect.x;
@@ -792,7 +791,14 @@ fn paint_image_layer_in_current_space<O: Observer>(
// Use a dedicated painting command for repeated images instead of recording a separate command for each
// instance of a repeated background, so the painter has the opportunity to optimize the painting of
// repeated images.
- let mut dest_rect = converter.rounded_device_rect(image_rect);
+ // NB: Tiles repeat in all directions at a whole device pixel size. Round the tile at the background position to
+ // get that size, as a tile any number of steps away can round to a different one. A lone tile has to stay
+ // where it is: only the tile that tile_count found covers the clip, and it is the only one that we paint.
+ let mut dest_rect = converter.rounded_device_rect(if tile_count == 1.0 {
+ image_rect
+ } else {
+ anchor_tile_rect
+ });
// If the image's dimensions were rounded to zero then they need to be restored to avoid a crash.
if dest_rect.width == 0 {
dest_rect.width = 1;
@@ -800,6 +806,16 @@ fn paint_image_layer_in_current_space<O: Observer>(
if dest_rect.height == 0 {
dest_rect.height = 1;
}
+ // NB: Whole tile steps don't change what repeats, but the painter works in floats, which can't hold the phase
+ // of a distant tile. Keep the origin next to the clip.
+ if tile_count != 1.0 {
+ if repeat_x {
+ dest_rect.x = first_tile_start(dest_rect.x, clip_rect.x, dest_rect.width);
+ }
+ if repeat_y {
+ dest_rect.y = first_tile_start(dest_rect.y, clip_rect.y, dest_rect.height);
+ }
+ }
if let crate::painting::image_content::ImageContent::Vector { content_identity, .. } = &facts.content {
if clip_rect.is_empty() {
return;
@@ -1036,15 +1052,24 @@ fn fraction_to_int(numerator: CssPixels, denominator: CssPixels) -> i32 {
numerator.div_as_fraction(denominator).to_int()
}
-fn ceil_fraction(numerator: CssPixels, denominator: CssPixels) -> CssPixels {
- if denominator.raw_value() == 0 {
- return CssPixels::from_raw(0);
+/// Moves a tile start by whole steps to the last position at or before the clip start. Integer math keeps this exact: a
+/// fixed point quotient truncates a gap below 1/64 of a step to zero steps.
+fn first_tile_start(tile_start: i32, clip_start: i32, step: i32) -> i32 {
+ if step <= 0 {
+ return tile_start;
}
- numerator.div_as_fraction(denominator).ceil()
+ let clip_start = i64::from(clip_start);
+ let clip_offset_from_tile = clip_start - i64::from(tile_start);
+ let first_start = clip_start - clip_offset_from_tile.rem_euclid(i64::from(step));
+ first_start.max(i64::from(i32::MIN)) as i32
}
-fn floor_css(value: CssPixels) -> CssPixels {
- value.floor()
+fn first_css_tile_start(tile_start: CssPixels, clip_start: CssPixels, step: CssPixels) -> CssPixels {
+ CssPixels::from_raw(first_tile_start(
+ tile_start.raw_value(),
+ clip_start.raw_value(),
+ step.raw_value(),
+ ))
}
fn source_rect_for_visible_image_part(
diff --git a/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-far-position-ref.html b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-far-position-ref.html
new file mode 100644
index 00000000000..5998aacf181
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-far-position-ref.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ div {
+ width: 90px;
+ height: 90px;
+ margin-bottom: 10px;
+ image-rendering: pixelated;
+ background: url(../../../data/smiley.png) 2px 2px / 3px 3px;
+ }
+</style>
+<div></div>
+<div></div>
diff --git a/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-fractional-size-phase-ref.html b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-fractional-size-phase-ref.html
new file mode 100644
index 00000000000..022f68e9bc3
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-fractional-size-phase-ref.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ div {
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 300px;
+ height: 300px;
+ border: 10px solid transparent;
+ }
+ .tiled {
+ background: linear-gradient(135deg, black 50%, white 50%) 0 0 / 33.75px 33.75px;
+ background-clip: padding-box;
+ }
+ .image {
+ background: url(../../../data/smiley.png) 0 0 / 33.5px 33.5px;
+ background-clip: padding-box;
+ }
+ .second {
+ left: 340px;
+ }
+ .cover {
+ border-color: white;
+ }
+</style>
+<div class="tiled"></div>
+<div class="cover"></div>
+<div class="second image"></div>
+<div class="second cover"></div>
diff --git a/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-single-tile-away-from-origin-ref.html b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-single-tile-away-from-origin-ref.html
new file mode 100644
index 00000000000..0cb8354e8c1
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-single-tile-away-from-origin-ref.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ div {
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 100.5px;
+ height: 100.5px;
+ }
+ .image {
+ background: url(../../../data/smiley.png) 0 0 / 100.5px 100.5px;
+ }
+ .vector {
+ left: 200px;
+ background: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><rect width='10' height='10' fill='green'/></svg>") 0 0 / 100.5px 100.5px;
+ }
+</style>
+<div class="image"></div>
+<div class="vector"></div>
diff --git a/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-thin-border-strip-ref.html b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-thin-border-strip-ref.html
new file mode 100644
index 00000000000..df462e11261
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css/backgrounds/background-tile-thin-border-strip-ref.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ div {
+ width: 204px;
+ height: 204px;
+ background: green;
+ }
+</style>
+<div></div>
diff --git a/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-far-position.html b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-far-position.html
new file mode 100644
index 00000000000..a344f7d0d80
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-far-position.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<!-- Tiles keep their phase when the background position is too far away for a float to hold it exactly. Both positions
+ here are 2px away from a multiple of the tile size. -->
+<link rel="match" href="../../../expected/css/backgrounds/background-tile-far-position-ref.html" />
+<style>
+ body { margin: 0; }
+ div {
+ width: 90px;
+ height: 90px;
+ margin-bottom: 10px;
+ image-rendering: pixelated;
+ }
+ .positive { background: url(../../../data/smiley.png) 16777217px 16777217px / 3px 3px; }
+ .negative { background: url(../../../data/smiley.png) -16777216px -16777216px / 3px 3px; }
+</style>
+<div class="positive"></div>
+<div class="negative"></div>
diff --git a/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-fractional-size-phase.html b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-fractional-size-phase.html
new file mode 100644
index 00000000000..2ec48ccb227
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-fractional-size-phase.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<!-- Tiles stay on exact multiples of a fractional tile size when painting starts one tile before the origin to cover
+ the border area. The cover hides the border area, as only the phase of the tiles inside it is under test. -->
+<link rel="match" href="../../../expected/css/backgrounds/background-tile-fractional-size-phase-ref.html" />
+<style>
+ body { margin: 0; }
+ div {
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 300px;
+ height: 300px;
+ border: 10px solid transparent;
+ }
+ .tiled {
+ background: linear-gradient(135deg, black 50%, white 50%) 0 0 / 33.75px 33.75px;
+ }
+ .image {
+ background: url(../../../data/smiley.png) 0 0 / 33.5px 33.5px;
+ }
+ .second {
+ left: 340px;
+ }
+ .cover {
+ border-color: white;
+ }
+</style>
+<div class="tiled"></div>
+<div class="cover"></div>
+<div class="second image"></div>
+<div class="second cover"></div>
diff --git a/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-single-tile-away-from-origin.html b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-single-tile-away-from-origin.html
new file mode 100644
index 00000000000..af3b3e6409a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-single-tile-away-from-origin.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<!-- A single tile covers the box, several steps away from the background position. The size of the tile is fractional,
+ and the tile has to stay where it is even though it is the only one painted. -->
+<link rel="match" href="../../../expected/css/backgrounds/background-tile-single-tile-away-from-origin-ref.html" />
+<style>
+ body { margin: 0; }
+ div {
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 100.5px;
+ height: 100.5px;
+ }
+ .image {
+ background: url(../../../data/smiley.png) 502.5px 0 / 100.5px 100.5px;
+ }
+ .vector {
+ left: 200px;
+ background: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><rect width='10' height='10' fill='green'/></svg>") 502.5px 0 / 100.5px 100.5px;
+ }
+</style>
+<div class="image"></div>
+<div class="vector"></div>
diff --git a/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-thin-border-strip.html b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-thin-border-strip.html
new file mode 100644
index 00000000000..cfbfc427e95
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css/backgrounds/background-tile-thin-border-strip.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<!-- The strips of a transparent border lie less than 1/64 of a tile away from the tile origin, and must still be
+ covered by the previous tile. -->
+<link rel="match" href="../../../expected/css/backgrounds/background-tile-thin-border-strip-ref.html" />
+<style>
+ body { margin: 0; }
+ div {
+ width: 200px;
+ height: 200px;
+ border: 2px solid transparent;
+ background: linear-gradient(green, green) 0 0 / calc(100% + 24px) calc(100% + 24px);
+ }
+</style>
+<div></div>
diff --git a/Tests/LibWeb/Screenshot/expected/border-radius.png b/Tests/LibWeb/Screenshot/expected/border-radius.png
index 68eba7f3056..6159632a8d5 100644
Binary files a/Tests/LibWeb/Screenshot/expected/border-radius.png and b/Tests/LibWeb/Screenshot/expected/border-radius.png differ
diff --git a/Tests/LibWeb/Screenshot/expected/css-background-clip-text.png b/Tests/LibWeb/Screenshot/expected/css-background-clip-text.png
index 1c38176682e..a5a7efd5978 100644
Binary files a/Tests/LibWeb/Screenshot/expected/css-background-clip-text.png and b/Tests/LibWeb/Screenshot/expected/css-background-clip-text.png differ
diff --git a/Tests/LibWeb/Screenshot/expected/css-backgrounds.png b/Tests/LibWeb/Screenshot/expected/css-backgrounds.png
index c45d4ee598c..8de75474fe1 100644
Binary files a/Tests/LibWeb/Screenshot/expected/css-backgrounds.png and b/Tests/LibWeb/Screenshot/expected/css-backgrounds.png differ