Truly independent web browser
LibWeb: Don’t move gradient stops outside 0%-100% onto the edges
sideshowbarker1d4668cc5c18
diff --git a/Libraries/LibCompositing/Rust/src/display_list/commands.rs b/Libraries/LibCompositing/Rust/src/display_list/commands.rsindex 946a4b599bc..ccc9a5b26d8 100644--- a/Libraries/LibCompositing/Rust/src/display_list/commands.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/commands.rs@@ -1476,6 +1476,8 @@ pub struct PaintConicGradient {pub rect: IntRect,pub start_angle: f32,pub color_stops: DisplayListGradientColorStops,+ pub first_stop_position: f32,+ pub repeat_length: f32,pub interpolation_method: GradientInterpolationMethod,pub position: IntPoint,pub compositing_and_blending_operator: CompositingAndBlendingOperator,@@ -1484,6 +1486,8 @@ display_list_record_fields!(PaintConicGradient {rect,start_angle,color_stops,+ first_stop_position,+ repeat_length,interpolation_method,position,compositing_and_blending_operatordiff --git a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rsindex 266c95003b2..eea0c83ec75 100644--- a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs+++ b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs@@ -119,6 +119,10 @@ pub struct LinearGradientData {pub struct ConicGradientData {pub start_angle: f32,pub color_stops: ColorStops,+ /// The fraction of a turn the stops' positions start from, and how many turns they run, as for a linear+ /// gradient's line.+ pub first_stop_position: f32,+ pub repeat_length: f32,pub interpolation_method: GradientInterpolationMethod,}@@ -805,6 +809,8 @@ impl DisplayListRecorder {rect,start_angle: data.start_angle,color_stops,+ first_stop_position: data.first_stop_position,+ repeat_length: data.repeat_length,interpolation_method: data.interpolation_method,position,compositing_and_blending_operator,diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/gradient_resolution.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/gradient_resolution.rsindex ed6d35db10e..ef5753ff83d 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/gradient_resolution.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/gradient_resolution.rs@@ -346,16 +346,32 @@ fn expand_color_stops_for_painting(replace_transition_hints_with_normal_color_stops(&expanded)}-fn finalized_color_stops(mut resolved_color_stops: ResolvedColorStopData) -> ColorStops {+/// The stops a gradient paints, with its transition hints replaced and a repeating gradient's stops repeated across its+/// range.+fn stops_for_painting(mut resolved_color_stops: ResolvedColorStopData) -> ResolvedColorStopData {if has_degenerate_repeat_length(&resolved_color_stops) {normalize_degenerate_repeating_gradient(&mut resolved_color_stops);}- // Expand color stops for painting (replace transition hints and expand repeat length)resolved_color_stops.list =expand_color_stops_for_painting(resolved_color_stops.list, resolved_color_stops.repeat_length);- to_color_stops(&resolved_color_stops.list, resolved_color_stops.repeating, |position| {- position- })+ resolved_color_stops+}++/// Where a non-repeating gradient's stops run from, and how far, where they're outside of 0 to 1: The painter moves+/// stops outside that range onto its ends, so such a gradient is painted across its stops' range instead, with its+/// stops placed along that. Every other gradient runs from 0 to 1.+fn stop_range(resolved_color_stops: &ResolvedColorStopData) -> (f32, f32) {+ if resolved_color_stops.repeating {+ return (0.0, 1.0);+ }+ let first = resolved_color_stops.list[0].position.min(0.0);+ let last = resolved_color_stops+ .list+ .last()+ .expect("a gradient has a stop")+ .position+ .max(1.0);+ (first, last - first)}fn to_color_stops(@@ -380,6 +396,102 @@ fn to_color_stops(}}+/// The color a gradient has at `position`, between its stops `previous` and `next`, interpolated in the gradient's+/// interpolation space, from the same colors the painter interpolates.+fn color_between_stops(+ previous: &GradientStop,+ next: &GradientStop,+ position: f32,+ interpolation: &GradientInterpolation<'_>,+) -> Color {+ let distance = next.position - previous.position;+ let progress = if distance > 0.0 {+ ((position - previous.position) / distance).clamp(0.0, 1.0)+ } else {+ 1.0+ };+ let resolved = |color: Color, percentage: f32| crate::css::color_interpolation::MixItem {+ color: crate::css::color_interpolation::ResolvedColor {+ color_type: crate::css::color_conversion::SRGB,+ components: [+ f32::from(color.red()) / 255.0,+ f32::from(color.green()) / 255.0,+ f32::from(color.blue()) / 255.0,+ f32::from(color.alpha()) / 255.0,+ ],+ missing: [false; 4],+ has_native_components: false,+ },+ percentage: f64::from(percentage * 100.0),+ };+ crate::css::color_interpolation::mix_colors(+ vec![resolved(previous.color, 1.0 - progress), resolved(next.color, progress)],+ &interpolation.method,+ 1.0,+ )+ .and_then(|mixed| to_color(&mixed, interpolation.color_input))+ .map_or_else(+ || previous.color.mixed_with(next.color, progress),+ |rgba| Color::from_rgba(rgba.r, rgba.g, rgba.b, rgba.a),+ )+}++/// How a gradient interpolates between its stops, for computing a color between two of them.+struct GradientInterpolation<'a> {+ method: StyleValueData,+ color_input: &'a ColorResolutionInput<'a>,+}++impl<'a> GradientInterpolation<'a> {+ fn new(+ color_interpolation_method: &RetainedStyleValueData,+ color_syntax: u8,+ color_input: &'a ColorResolutionInput<'a>,+ ) -> Self {+ use crate::css::css_enums::rectangular_color_space;+ // NB: An omitted method is the one gradient_interpolation_method() gives the painter: sRGB for a gradient+ // written with legacy colors only, and Oklab otherwise.+ let method =+ color_interpolation_method+ .optional_data()+ .cloned()+ .unwrap_or(StyleValueData::ColorInterpolationMethod {+ is_polar: false,+ color_space: if color_syntax == 0 {+ rectangular_color_space::SRGB+ } else {+ rectangular_color_space::OKLAB+ },+ hue_interpolation_method: 0,+ });+ Self { method, color_input }+ }+}++/// Drops the stops of a non-repeating gradient that are below 0, putting a stop at 0 in their place, with the color the+/// gradient has there. A radial gradient paints nothing inside its center, but the painter would move such stops onto+/// it, colors and all.+fn clip_stops_below_zero(stops: Vec<GradientStop>, interpolation: &GradientInterpolation<'_>) -> Vec<GradientStop> {+ let edge_stop = |color: Color, position: f32| GradientStop {+ color,+ position,+ transition_hint: None,+ };+ let mut stops = stops;+ if stops.first().is_some_and(|stop| stop.position < 0.0) {+ stops = match stops.iter().position(|stop| stop.position >= 0.0) {+ Some(first_in_range) => {+ let edge = color_between_stops(&stops[first_in_range - 1], &stops[first_in_range], 0.0, interpolation);+ std::iter::once(edge_stop(edge, 0.0))+ .chain(stops[first_in_range..].iter().copied())+ .collect()+ }+ None => vec![edge_stop(stops.last().expect("a gradient has a stop").color, 0.0)],+ };+ }+ stops+}+fn resolve_color_stop_positions(color_stop_list: &[RetainedColorStop],color_input: &ColorResolutionInput<'_>,@@ -727,11 +839,9 @@ fn resolve_linear_gradient_paint(// Replace transition hints for painting; keep repeat_length for Skia's native tilingresolved_color_stops.list = replace_transition_hints_with_normal_color_stops(&resolved_color_stops.list);- let repeat_length = resolved_color_stops.repeat_length.unwrap_or(1.0);- let first_stop_position = if resolved_color_stops.repeat_length.is_some() {- resolved_color_stops.list[0].position- } else {- 0.0+ let (first_stop_position, repeat_length) = match resolved_color_stops.repeat_length {+ Some(repeat_length) => (resolved_color_stops.list[0].position, repeat_length),+ None => stop_range(&resolved_color_stops),};let color_stops = to_color_stops(&resolved_color_stops.list, resolved_color_stops.repeating, |position| {(position - first_stop_position) / repeat_length@@ -758,7 +868,11 @@ fn resolve_conic_gradient_paint() -> (ConicGradientData, CssPixelPoint) {let resolved_color_stops =resolve_color_stop_positions(color_stop_list, color_input, stop_angle_fraction_of_turn, repeating);- let color_stops = finalized_color_stops(resolved_color_stops);+ let resolved_color_stops = stops_for_painting(resolved_color_stops);+ let (first_stop_position, repeat_length) = stop_range(&resolved_color_stops);+ let color_stops = to_color_stops(&resolved_color_stops.list, resolved_color_stops.repeating, |position| {+ (position - first_stop_position) / repeat_length+ });let start_angle = match from_angle.optional_data() {Some(from_angle) => angle_to_degrees(from_angle) as f32,None => 0.0,@@ -766,6 +880,8 @@ fn resolve_conic_gradient_paint(let data = ConicGradientData {start_angle,color_stops,+ first_stop_position,+ repeat_length,interpolation_method: gradient_interpolation_method(color_interpolation_method, color_syntax),};(data, position_resolved(position.optional_data(), gradient_box))@@ -801,7 +917,29 @@ fn resolve_radial_gradient_paint(},repeating,);- let color_stops = finalized_color_stops(resolved_color_stops);+ // NB: Stops past the ending shape are reached by the rays beyond it, so rather than being clipped, they scale the+ // shape the painter places the stops in.+ let interpolation = GradientInterpolation::new(color_interpolation_method, color_syntax, color_input);+ let mut resolved_color_stops = stops_for_painting(resolved_color_stops);+ if !resolved_color_stops.repeating {+ resolved_color_stops.list = clip_stops_below_zero(resolved_color_stops.list, &interpolation);+ }+ let mut color_stops = to_color_stops(&resolved_color_stops.list, resolved_color_stops.repeating, |position| {+ position+ });+ let mut gradient_size = gradient_size;+ if let Some(&last) = color_stops.positions.last()+ && last > 1.0+ && !color_stops.repeating+ {+ for position in &mut color_stops.positions {+ *position /= last;+ }+ gradient_size = CssPixelSize::new(+ gradient_size.width.scaled(f64::from(last)),+ gradient_size.height.scaled(f64::from(last)),+ );+ }let data = RadialGradientData {color_stops,interpolation_method: gradient_interpolation_method(color_interpolation_method, color_syntax),diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cppindex 7af849833dd..ce34fcdb85f 100644--- a/Services/Compositor/DisplayListPlayerSkia.cpp+++ b/Services/Compositor/DisplayListPlayerSkia.cpp@@ -1400,7 +1400,10 @@ void DisplayListPlayerSkia::play_command(PaintConicGradient const& command)auto color_space = SkColorSpace::MakeSRGB();auto interpolation = to_skia_interpolation(command.interpolation_method);SkGradient gradient { SkGradient::Colors { { colors.data(), colors.size() }, { color_stop_positions.data(), color_stop_positions.size() }, SkTileMode::kRepeat, color_space }, interpolation };- auto shader = SkShaders::SweepGradient(to_skia_point(center), 0, 360, gradient, &matrix);+ // The stops' positions run from the first stop's fraction of a turn across as many turns as the gradient's span.+ auto start_degrees = command.first_stop_position * 360;+ auto end_degrees = (command.first_stop_position + command.repeat_length) * 360;+ auto shader = SkShaders::SweepGradient(to_skia_point(center), start_degrees, end_degrees, gradient, &matrix);SkPaint paint;paint.setDither(true);diff --git a/Tests/LibWeb/Ref/expected/css-gradient-stops-outside-range-ref.html b/Tests/LibWeb/Ref/expected/css-gradient-stops-outside-range-ref.htmlnew file mode 100644index 00000000000..a8d31f2ccd0--- /dev/null+++ b/Tests/LibWeb/Ref/expected/css-gradient-stops-outside-range-ref.html@@ -0,0 +1,19 @@+<!DOCTYPE html>+<style>+ body {+ margin: 0;+ }+ div {+ float: left;+ width: 100px;+ height: 100px;+ margin: 0 10px 10px 0;+ }+</style>+<div style="background: linear-gradient(rgb(100, 100, 100), rgb(200, 200, 200))"></div>+<div style="background: linear-gradient(black, rgb(100, 100, 100))"></div>+<div style="background: linear-gradient(90deg, rgb(50, 50, 50), rgb(100, 100, 100))"></div>+<div style="background: radial-gradient(circle 50px, rgb(100, 100, 100), rgb(200, 200, 200))"></div>+<div style="background: radial-gradient(circle 50px, black, rgb(200, 200, 200))"></div>+<div style="background: conic-gradient(rgb(100, 100, 100), rgb(200, 200, 200))"></div>+<div style="background: conic-gradient(black, rgb(100, 100, 100))"></div>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-images/reference/100x100-blue.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-images/reference/100x100-blue.htmlnew file mode 100644index 00000000000..60015b014d7--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-images/reference/100x100-blue.html@@ -0,0 +1,2 @@+<!doctype html>+<div style="width: 100px;height: 100px;background-color: blue;"></div>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-images/reference/200x200-blue-black-green-red.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-images/reference/200x200-blue-black-green-red.htmlnew file mode 100644index 00000000000..04322b066c3--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-images/reference/200x200-blue-black-green-red.html@@ -0,0 +1,22 @@+<!doctype html>+<meta charset="utf-8">+<style>+ #box {+ width: 200px;+ height: 200px;+ }+ #top {+ border-left: 100px solid blue;+ border-right: 100px solid black;+ height: 100px;+ }+ #bottom {+ border-left: 100px solid green;+ border-right: 100px solid red;+ height: 100px;+ }+</style>+<div id="box">+ <div id="top"></div>+ <div id="bottom"></div>+</div>diff --git a/Tests/LibWeb/Ref/input/css-gradient-stops-outside-range.html b/Tests/LibWeb/Ref/input/css-gradient-stops-outside-range.htmlnew file mode 100644index 00000000000..5a56c6a8e74--- /dev/null+++ b/Tests/LibWeb/Ref/input/css-gradient-stops-outside-range.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<link rel="match" href="../expected/css-gradient-stops-outside-range-ref.html" />+<meta name="fuzzy" content="maxDifference=0-2;totalPixels=0-60000" />+<style>+ body {+ margin: 0;+ }+ div {+ float: left;+ width: 100px;+ height: 100px;+ margin: 0 10px 10px 0;+ }+</style>+<!-- A gradient paints the part of its stops' range that's inside 0%-100%, with the colors it has there. -->+<div style="background: linear-gradient(black -100%, rgb(200, 200, 200))"></div>+<div style="background: linear-gradient(black, rgb(200, 200, 200) 200%)"></div>+<div style="background: linear-gradient(90deg, black -100%, rgb(200, 200, 200) 300%)"></div>+<div style="background: radial-gradient(circle 50px, black -100%, rgb(200, 200, 200))"></div>+<div style="background: radial-gradient(circle 25px, black, rgb(200, 200, 200) 200%)"></div>+<div style="background: conic-gradient(black -100%, rgb(200, 200, 200))"></div>+<div style="background: conic-gradient(black, rgb(200, 200, 200) 200%)"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-conic-2.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-conic-2.htmlnew file mode 100644index 00000000000..5b38c3ff064--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-conic-2.html@@ -0,0 +1,16 @@+<!doctype html>+<meta charset="utf-8">+<title>Conic gradient stop normalization</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-images-4/#conic-gradients">+<meta name="assert" content="Rendering of conic-gradient with normalized color stops">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-10000">+<link rel="match" href="../../../../expected/wpt-import/css/css-images/reference/100x100-blue.html">+<style>+ #gradient {+ width: 100px;+ height: 100px;+ background-image: conic-gradient(blue 150%, red 150%);+ }+</style>+<div id="gradient"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-conic.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-conic.htmlnew file mode 100644index 00000000000..5e6f30552e0--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-conic.html@@ -0,0 +1,16 @@+<!doctype html>+<meta charset="utf-8">+<title>Conic gradient stop normalization</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-images-4/#conic-gradients">+<meta name="assert" content="Rendering of conic-gradient with normalized color stops">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-10000">+<link rel="match" href="../../../../expected/wpt-import/css/css-images/reference/100x100-blue.html">+<style>+ #gradient {+ width: 100px;+ height: 100px;+ background-image: conic-gradient(green -50%, blue -50%);+ }+</style>+<div id="gradient"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-linear-2.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-linear-2.htmlnew file mode 100644index 00000000000..3e7f33cb8b9--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-linear-2.html@@ -0,0 +1,16 @@+<!doctype html>+<meta charset="utf-8">+<title>Linear gradient stop normalization</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-images-3/#linear-gradients">+<meta name="assert" content="Rendering of linear-gradient with normalized color stops">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-40000">+<link rel="match" href="../../../../expected/wpt-import/css/css-images/reference/100x100-blue.html">+<style>+ #gradient {+ width: 100px;+ height: 100px;+ background-image: linear-gradient(blue 150%, red 150%);+ }+</style>+<div id="gradient"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-linear.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-linear.htmlnew file mode 100644index 00000000000..664237c5fd0--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-linear.html@@ -0,0 +1,16 @@+<!doctype html>+<meta charset="utf-8">+<title>Linear gradient stop normalization</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-images-3/#linear-gradients">+<meta name="assert" content="Rendering of linear-gradient with normalized color stops">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-40000">+<link rel="match" href="../../../../expected/wpt-import/css/css-images/reference/100x100-blue.html">+<style>+ #gradient {+ width: 100px;+ height: 100px;+ background-image: linear-gradient(green -50%, blue -50%);+ }+</style>+<div id="gradient"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-radial-2.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-radial-2.htmlnew file mode 100644index 00000000000..8606e4c9c83--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-radial-2.html@@ -0,0 +1,16 @@+<!doctype html>+<meta charset="utf-8">+<title>Radial gradient stop normalization</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-images-3/#radial-gradients">+<meta name="assert" content="Rendering of radial-gradient with normalized color stops">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-40000">+<link rel="match" href="../../../../expected/wpt-import/css/css-images/reference/100x100-blue.html">+<style>+ #gradient {+ width: 100px;+ height: 100px;+ background-image: radial-gradient(blue 150%, red 150%);+ }+</style>+<div id="gradient"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-radial.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-radial.htmlnew file mode 100644index 00000000000..d5dca626439--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/normalization-radial.html@@ -0,0 +1,16 @@+<!doctype html>+<meta charset="utf-8">+<title>Radial gradient stop normalization</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-images-3/#radial-gradients">+<meta name="assert" content="Rendering of radial-gradient with normalized color stops">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-40000">+<link rel="match" href="../../../../expected/wpt-import/css/css-images/reference/100x100-blue.html">+<style>+ #gradient {+ width: 100px;+ height: 100px;+ background-image: radial-gradient(green -50%, blue -50%);+ }+</style>+<div id="gradient"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-images/out-of-range-color-stop-conic.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/out-of-range-color-stop-conic.htmlnew file mode 100644index 00000000000..be1e8560fdf--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-images/out-of-range-color-stop-conic.html@@ -0,0 +1,16 @@+<!doctype html>+<meta charset="utf-8">+<title>Conic gradient with out-of-range stops</title>+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">+<link rel="help" href="https://drafts.csswg.org/css-images-4/#conic-gradients">+<meta name="assert" content="Rendering of conic-gradient with stops positioned outside of [0, 1]">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-40000">+<link rel="match" href="../../../../expected/wpt-import/css/css-images/reference/200x200-blue-black-green-red.html">+<style>+ #gradient {+ width: 200px;+ height: 200px;+ background-image: conic-gradient(orange -50% -25%, black -25% 25%, red 25% 50%, green 50% 75%, blue 75% 125%, purple 125% 150%);+ }+</style>+<div id="gradient"></div>