Skip to content

Truly independent web browser

LibWeb: Leave content-sized inline SVG as authored under force dark

sideshowbarker1aa04a5bac0d
diff --git a/Libraries/LibCompositing/Rust/src/force_dark.rs b/Libraries/LibCompositing/Rust/src/force_dark.rs
index 2cd3bf05d52..7a65be3b805 100644
--- a/Libraries/LibCompositing/Rust/src/force_dark.rs
+++ b/Libraries/LibCompositing/Rust/src/force_dark.rs
@@ -405,6 +405,12 @@ pub fn image_size_allows_classification(dest_width_css: f32, dest_height_css: f3
source_size.0 <= MAX_SEPARATOR_SOURCE_LENGTH || source_size.1 <= MAX_SEPARATOR_SOURCE_LENGTH
}
+/// Whether the content of an SVG root with a border box this size may be darkened: Only an icon-sized one's is, as for
+/// images. The size is in CSS pixels, so page zoom can't flip it.
+pub fn svg_root_size_allows_darkening(width_css: f32, height_css: f32) -> bool {
+ width_css <= MAX_ICON_LENGTH_CSS && height_css <= MAX_ICON_LENGTH_CSS
+}
+
/// The role an image draw takes: the one the call site meant, or None for a photo-sized image. The gate is sized in
/// CSS pixels so page zoom can't flip an icon into a photo.
pub fn role_for_image(
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
index e50c6f1421a..81841a7fe7c 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
@@ -469,6 +469,37 @@ pub(crate) fn paint_text_after_children<O: Observer>(
}
}
+/// The force-dark role of the fills and strokes of the SVG box `paintable`: that of SVG content, or none where the
+/// outermost svg element of the SVG inline in the page is bigger than an icon. Its content is then more likely an
+/// illustration, a chart or a QR code than line art, and it stays as authored.
+fn svg_content_force_dark_role(source: &impl PaintRead, paintable: NodeSlotId) -> ForceDarkRole {
+ let mut root = None;
+ let mut node = Some(paintable);
+ while let Some(current) = node {
+ match source.node_kind_if_live(current) {
+ Some(NodeKind::SVGSVGBox) => root = Some(current),
+ Some(kind) if crate::painting::node_painting::is_svg(kind) => {}
+ // The outermost svg element is the root element of an SVG document, such as an SVG image's, whose content
+ // darkens whatever its size.
+ Some(NodeKind::Viewport) => return ForceDarkRole::Svg,
+ _ => break,
+ }
+ node = source.node_parent_if_live(current);
+ }
+ let Some(root) = root else {
+ return ForceDarkRole::Svg;
+ };
+ let border_box = crate::painting::paintable_geometry::absolute_border_box_rect(source, root);
+ if crate::painting::force_dark::svg_root_size_allows_darkening(
+ border_box.width.to_float(),
+ border_box.height.to_float(),
+ ) {
+ ForceDarkRole::Svg
+ } else {
+ ForceDarkRole::None
+ }
+}
+
fn paint_path_parts<O: Observer>(
recorder: &mut PaintRecorder<'_, O>,
paintable: NodeSlotId,
@@ -563,6 +594,7 @@ fn paint_path_parts<O: Observer>(
}
return;
}
+ let svg_role = svg_content_force_dark_role(recorder.source, paintable);
let geometry =
crate::painting::paintable_geometry::committed_svg_geometry(recorder.source, paintable).unwrap_or_default();
@@ -596,7 +628,7 @@ fn paint_path_parts<O: Observer>(
}
if let Some(paint_style) = paint_server_style(recorder, paintable, false, &paint_context) {
recorder.recorder.fill_path(FillPathParams {
- force_dark_role: ForceDarkRole::Svg,
+ force_dark_role: svg_role,
path,
opacity: fill_opacity,
paint_style_or_color: PaintStyleOrColor::PaintStyle(paint_style),
@@ -605,7 +637,7 @@ fn paint_path_parts<O: Observer>(
});
} else if let Some(fill_color) = facts.fill_color {
recorder.recorder.fill_path(FillPathParams {
- force_dark_role: ForceDarkRole::Svg,
+ force_dark_role: svg_role,
path,
opacity: 1.0,
paint_style_or_color: PaintStyleOrColor::Color(
@@ -649,7 +681,7 @@ fn paint_path_parts<O: Observer>(
if let Some(paint_style) = paint_server_style(recorder, paintable, true, &paint_context) {
recorder.recorder.stroke_path(StrokePathParams {
- force_dark_role: ForceDarkRole::Svg,
+ force_dark_role: svg_role,
cap_style: facts.cap_style,
join_style: facts.join_style,
miter_limit: facts.miter_limit,
@@ -663,7 +695,7 @@ fn paint_path_parts<O: Observer>(
});
} else if let Some(stroke_color) = facts.stroke_color {
recorder.recorder.stroke_path(StrokePathParams {
- force_dark_role: ForceDarkRole::Svg,
+ force_dark_role: svg_role,
cap_style: facts.cap_style,
join_style: facts.join_style,
miter_limit: facts.miter_limit,
@@ -696,6 +728,7 @@ fn paint_color_glyph_runs<O: Observer>(
use crate::painting::display_list::commands::{DisplayListGlyph, FontResourceId};
use crate::painting::display_list::recorder::GlyphRunForRecording;
+ let svg_role = svg_content_force_dark_role(recorder.source, paintable);
let Some(style) = recorder.source.node_style_if_live(paintable) else {
return;
};
@@ -726,7 +759,7 @@ fn paint_color_glyph_runs<O: Observer>(
f64::from(device_scale),
libgfx_rust::GlyphRunOrientation::Horizontal,
device_rect,
- ForceDarkRole::Svg,
+ svg_role,
);
}
}
diff --git a/Tests/LibWeb/Ref/expected/force-dark-darkens-content-sized-svg-images-notref.html b/Tests/LibWeb/Ref/expected/force-dark-darkens-content-sized-svg-images-notref.html
new file mode 100644
index 00000000000..73e6e04b955
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/force-dark-darkens-content-sized-svg-images-notref.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<style>
+ body { margin: 0; background: #808080; }
+ img { width: 100px; height: 100px; }
+</style>
+<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iI2Y2ZDJkZCIvPjxyZWN0IHg9IjI1IiB5PSIyNSIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjMjAyMDIwIi8+PC9zdmc+">
+<script>
+ // The same page with force-dark off. The darkened test page must not render like this.
+ if (window.internals)
+ internals.setForceDarkEnabled(false);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/expected/force-dark-darkens-svg-images-like-the-page-ref.html b/Tests/LibWeb/Ref/expected/force-dark-darkens-svg-images-like-the-page-ref.html
index 43e53f1d7d1..a900a94d611 100644
--- a/Tests/LibWeb/Ref/expected/force-dark-darkens-svg-images-like-the-page-ref.html
+++ b/Tests/LibWeb/Ref/expected/force-dark-darkens-svg-images-like-the-page-ref.html
@@ -4,8 +4,8 @@
body { margin: 0; background: #808080; }
svg { position: absolute; top: 0; }
</style>
-<svg style="left: 0" width="100" height="100"><rect width="100" height="100" fill="#f6d2dd"/><rect x="25" y="25" width="50" height="50" fill="#202020"/></svg>
-<svg style="left: 100px" width="100" height="100"><rect width="100" height="100" fill="#f6d2dd"/><rect x="25" y="25" width="50" height="50" fill="#202020"/></svg>
+<svg style="left: 0" width="60" height="60"><rect width="60" height="60" fill="#f6d2dd"/><rect x="15" y="15" width="30" height="30" fill="#202020"/></svg>
+<svg style="left: 60px" width="60" height="60"><rect width="60" height="60" fill="#f6d2dd"/><rect x="15" y="15" width="30" height="30" fill="#202020"/></svg>
<script>
if (window.internals)
internals.setForceDarkEnabled(true);
diff --git a/Tests/LibWeb/Ref/expected/force-dark-inverts-path-gradients-notref.html b/Tests/LibWeb/Ref/expected/force-dark-inverts-path-gradients-notref.html
index fff8d607cf2..c4f52876285 100644
--- a/Tests/LibWeb/Ref/expected/force-dark-inverts-path-gradients-notref.html
+++ b/Tests/LibWeb/Ref/expected/force-dark-inverts-path-gradients-notref.html
@@ -3,14 +3,14 @@
<style>
body { margin: 0; background: #808080; }
</style>
-<svg width="200" height="100">
+<svg width="64" height="32">
<defs>
<linearGradient id="g" x1="0" y1="0" x2="1" y2="0">
<stop offset="0" stop-color="#000000"></stop>
<stop offset="1" stop-color="#303030"></stop>
</linearGradient>
</defs>
- <path d="M 10 10 L 190 10 L 190 90 L 10 90 Z" fill="url(#g)" stroke="url(#g)" stroke-width="8"></path>
+ <path d="M 4 4 L 60 4 L 60 28 L 4 28 Z" fill="url(#g)" stroke="url(#g)" stroke-width="4"></path>
</svg>
<script>
// The same page with force-dark off. The darkened test page must not render like this.
diff --git a/Tests/LibWeb/Ref/expected/force-dark-leaves-content-sized-inline-svg-alone-ref.html b/Tests/LibWeb/Ref/expected/force-dark-leaves-content-sized-inline-svg-alone-ref.html
new file mode 100644
index 00000000000..3f64513e4cc
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/force-dark-leaves-content-sized-inline-svg-alone-ref.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<style>
+ body { margin: 0; background: #808080; }
+ svg { display: block; }
+</style>
+<svg width="100" height="100">
+ <rect width="100" height="100" fill="#ffffff"></rect>
+ <rect x="10" y="10" width="30" height="30" fill="#000000"></rect>
+ <rect x="60" y="10" width="30" height="30" fill="#000000"></rect>
+ <rect x="10" y="60" width="30" height="30" fill="#000000"></rect>
+ <path d="M 60 60 L 90 60 L 90 90 Z" fill="none" stroke="#000000" stroke-width="4"></path>
+</svg>
+<svg width="65" height="20"><rect width="65" height="20" fill="#000000"></rect></svg>
+<svg width="20" height="65"><rect width="20" height="65" fill="#000000"></rect></svg>
+<svg width="60" height="60" style="padding: 4px"><rect width="60" height="60" fill="#000000"></rect></svg>
+<svg width="200" height="50">
+ <svg width="40" height="40"><rect width="40" height="40" fill="#000000"></rect></svg>
+</svg>
+<script>
+ // The same page with force-dark off. It must render identically to the test.
+ if (window.internals)
+ internals.setForceDarkEnabled(false);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/force-dark-darkens-content-sized-svg-images.html b/Tests/LibWeb/Ref/input/force-dark-darkens-content-sized-svg-images.html
new file mode 100644
index 00000000000..e14bda1abcf
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/force-dark-darkens-content-sized-svg-images.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="mismatch" href="../expected/force-dark-darkens-content-sized-svg-images-notref.html">
+<style>
+ body { margin: 0; background: #808080; }
+ img { width: 100px; height: 100px; }
+</style>
+<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iI2Y2ZDJkZCIvPjxyZWN0IHg9IjI1IiB5PSIyNSIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjMjAyMDIwIi8+PC9zdmc+">
+<script>
+ // An SVG image bigger than an icon still darkens its content, unlike an inline SVG of that size.
+ if (window.internals)
+ internals.setForceDarkEnabled(true);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/force-dark-darkens-svg-images-like-the-page.html b/Tests/LibWeb/Ref/input/force-dark-darkens-svg-images-like-the-page.html
index 8170f1a3477..15f839d8d4c 100644
--- a/Tests/LibWeb/Ref/input/force-dark-darkens-svg-images-like-the-page.html
+++ b/Tests/LibWeb/Ref/input/force-dark-darkens-svg-images-like-the-page.html
@@ -3,12 +3,12 @@
<link rel="match" href="../expected/force-dark-darkens-svg-images-like-the-page-ref.html">
<style>
body { margin: 0; background: #808080; }
- div, img { position: absolute; top: 0; width: 100px; height: 100px; }
- div { left: 0; background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iI2Y2ZDJkZCIvPjxyZWN0IHg9IjI1IiB5PSIyNSIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjMjAyMDIwIi8+PC9zdmc+"); }
- img { left: 100px; }
+ div, img { position: absolute; top: 0; width: 60px; height: 60px; }
+ div { left: 0; background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2MCIgaGVpZ2h0PSI2MCI+PHJlY3Qgd2lkdGg9IjYwIiBoZWlnaHQ9IjYwIiBmaWxsPSIjZjZkMmRkIi8+PHJlY3QgeD0iMTUiIHk9IjE1IiB3aWR0aD0iMzAiIGhlaWdodD0iMzAiIGZpbGw9IiMyMDIwMjAiLz48L3N2Zz4="); }
+ img { left: 60px; }
</style>
<div></div>
-<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iI2Y2ZDJkZCIvPjxyZWN0IHg9IjI1IiB5PSIyNSIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjMjAyMDIwIi8+PC9zdmc+">
+<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2MCIgaGVpZ2h0PSI2MCI+PHJlY3Qgd2lkdGg9IjYwIiBoZWlnaHQ9IjYwIiBmaWxsPSIjZjZkMmRkIi8+PHJlY3QgeD0iMTUiIHk9IjE1IiB3aWR0aD0iMzAiIGhlaWdodD0iMzAiIGZpbGw9IiMyMDIwMjAiLz48L3N2Zz4=">
<script>
// An SVG image's content darkens with the page that draws it: Drawn as a background image or as an img, it lands
// on the same colors as the same SVG inline in the page.
diff --git a/Tests/LibWeb/Ref/input/force-dark-inverts-path-gradients.html b/Tests/LibWeb/Ref/input/force-dark-inverts-path-gradients.html
index a855aa3c481..922e6338dca 100644
--- a/Tests/LibWeb/Ref/input/force-dark-inverts-path-gradients.html
+++ b/Tests/LibWeb/Ref/input/force-dark-inverts-path-gradients.html
@@ -4,17 +4,18 @@
<style>
body { margin: 0; background: #808080; }
</style>
-<svg width="200" height="100">
+<svg width="64" height="32">
<defs>
<linearGradient id="g" x1="0" y1="0" x2="1" y2="0">
<stop offset="0" stop-color="#000000"></stop>
<stop offset="1" stop-color="#303030"></stop>
</linearGradient>
</defs>
- <path d="M 10 10 L 190 10 L 190 90 L 10 90 Z" fill="url(#g)" stroke="url(#g)" stroke-width="8"></path>
+ <path d="M 4 4 L 60 4 L 60 28 L 4 28 Z" fill="url(#g)" stroke="url(#g)" stroke-width="4"></path>
</svg>
<script>
// A gradient-painted path resolves stop by stop, like a gradient-painted rectangle: dark stops read as line art and must lighten.
+ // The SVG is icon-sized, since the content of a bigger one stays as authored.
if (window.internals)
internals.setForceDarkEnabled(true);
requestAnimationFrame(() => {
diff --git a/Tests/LibWeb/Ref/input/force-dark-leaves-content-sized-inline-svg-alone.html b/Tests/LibWeb/Ref/input/force-dark-leaves-content-sized-inline-svg-alone.html
new file mode 100644
index 00000000000..0a64f66514f
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/force-dark-leaves-content-sized-inline-svg-alone.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/force-dark-leaves-content-sized-inline-svg-alone-ref.html">
+<style>
+ body { margin: 0; background: #808080; }
+ svg { display: block; }
+</style>
+<svg width="100" height="100">
+ <rect width="100" height="100" fill="#ffffff"></rect>
+ <rect x="10" y="10" width="30" height="30" fill="#000000"></rect>
+ <rect x="60" y="10" width="30" height="30" fill="#000000"></rect>
+ <rect x="10" y="60" width="30" height="30" fill="#000000"></rect>
+ <path d="M 60 60 L 90 60 L 90 90 Z" fill="none" stroke="#000000" stroke-width="4"></path>
+</svg>
+<svg width="65" height="20"><rect width="65" height="20" fill="#000000"></rect></svg>
+<svg width="20" height="65"><rect width="20" height="65" fill="#000000"></rect></svg>
+<svg width="60" height="60" style="padding: 4px"><rect width="60" height="60" fill="#000000"></rect></svg>
+<svg width="200" height="50">
+ <svg width="40" height="40"><rect width="40" height="40" fill="#000000"></rect></svg>
+</svg>
+<script>
+ // An inline SVG wider or taller than an icon is content, like a QR code, so its fills and strokes stay as
+ // authored: Inverting its dark parts and not its light ones would leave it unreadable. A nested svg element
+ // isn't a root of its own, so its size doesn't matter; its outermost svg element's does, and
+ // that by its border box.
+ if (window.internals)
+ internals.setForceDarkEnabled(true);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>