Skip to content

Truly independent web browser

LibWeb: Paint a focused area's focus ring from the shape Rust built

Aliaksandr Kalenikf4931beb695d
diff --git a/Libraries/LibWeb/HTML/HTMLAreaElement.cpp b/Libraries/LibWeb/HTML/HTMLAreaElement.cpp
index 5dbae318a1a..31b62621226 100644
--- a/Libraries/LibWeb/HTML/HTMLAreaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLAreaElement.cpp
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibGfx/Path.h>
#include <LibWeb/ARIA/Roles.h>
#include <LibWeb/DOM/DOMTokenList.h>
#include <LibWeb/DOM/Event.h>
@@ -105,104 +104,6 @@ Vector<double> HTMLAreaElement::shape_coords() const
return parse_list_of_floating_point_numbers(coords_attribute->utf16_view());
}
-// https://html.spec.whatwg.org/multipage/image-maps.html#image-map-processing-model
-// FIXME: The Rust image map area column builds the same shape for hit testing. Paint a focused area's outline from
-// that one too, so the two cannot disagree.
-Optional<Gfx::Path> HTMLAreaElement::shape_path(CSSPixelSize image_size) const
-{
- // Each area element in areas must be processed as follows to obtain a shape to layer onto the image:
-
- // 1. Find the state that the element's shape attribute represents.
- auto shape = shape_state();
-
- // 2. Use the rules for parsing a list of floating-point numbers to parse the element's coords attribute, if it
- // is present, and let the coords list be the result. If the attribute is absent, let the coords list be the
- // empty list.
- auto coords = shape_coords();
-
- auto vertex = [&](size_t index) {
- return Gfx::FloatPoint { static_cast<float>(coords[2 * index]), static_cast<float>(coords[2 * index + 1]) };
- };
-
- auto rectangle_path = [](Gfx::FloatPoint top_left, Gfx::FloatPoint bottom_right) {
- Gfx::Path path;
- path.move_to(top_left);
- path.line_to({ bottom_right.x(), top_left.y() });
- path.line_to(bottom_right);
- path.line_to({ top_left.x(), bottom_right.y() });
- path.close();
- return path;
- };
-
- // 3. If the number of items in the coords list is less than the minimum number given for the area element's
- // current state, as per the following table, then the shape is empty; return.
- // 4. Check for excess items in the coords list as per the entry in the following list corresponding to the
- // shape attribute's state:
- // NB: Excess items require no handling, because each shape reads only the items it requires.
- // 8. Now, the shape represented by the element is the one described for the entry in the list below
- // corresponding to the state of the shape attribute:
- switch (shape) {
- case ShapeState::Circle: {
- if (coords.size() < 3)
- return {};
-
- // 7. If the shape attribute represents the circle state, and the third number in the list is less than or
- // equal to zero, then the shape is empty; return.
- if (coords[2] <= 0)
- return {};
-
- // Let x be the first number in coords, y be the second number, and r be the third number.
- // The shape is a circle whose center is x CSS pixels from the left edge of the image and y CSS pixels from
- // the top edge of the image, and whose radius is r CSS pixels.
- auto x = static_cast<float>(coords[0]);
- auto y = static_cast<float>(coords[1]);
- auto radius = static_cast<float>(coords[2]);
- Gfx::Path path;
- path.move_to({ x + radius, y });
- path.arc_to({ x, y + radius }, radius, false, true);
- path.arc_to({ x - radius, y }, radius, false, true);
- path.arc_to({ x, y - radius }, radius, false, true);
- path.arc_to({ x + radius, y }, radius, false, true);
- path.close();
- return path;
- }
- case ShapeState::Default:
- // The shape is a rectangle that exactly covers the entire image.
- return rectangle_path({ 0, 0 }, { static_cast<float>(image_size.width().to_double()), static_cast<float>(image_size.height().to_double()) });
- case ShapeState::Polygon: {
- if (coords.size() < 6)
- return {};
-
- Gfx::Path path;
- path.move_to(vertex(0));
- for (size_t i = 1; i < coords.size() / 2; ++i)
- path.line_to(vertex(i));
- path.close();
- return path;
- }
- case ShapeState::Rectangle: {
- if (coords.size() < 4)
- return {};
-
- // 5. If the shape attribute represents the rectangle state, and the first number in the list is numerically
- // greater than the third number in the list, then swap those two numbers around.
- if (coords[0] > coords[2])
- swap(coords[0], coords[2]);
-
- // 6. If the shape attribute represents the rectangle state, and the second number in the list is
- // numerically greater than the fourth number in the list, then swap those two numbers around.
- if (coords[1] > coords[3])
- swap(coords[1], coords[3]);
-
- // The shape is a rectangle whose top-left corner is given by the coordinate (x1, y1) and whose bottom right
- // corner is given by the coordinate (x2, y2), those coordinates being interpreted as CSS pixels from the
- // top left corner of the image.
- return rectangle_path(vertex(0), vertex(1));
- }
- }
- VERIFY_NOT_REACHED();
-}
-
// https://html.spec.whatwg.org/multipage/image-maps.html#dom-area-rellist
GC::Ref<DOM::DOMTokenList> HTMLAreaElement::rel_list()
{
diff --git a/Libraries/LibWeb/HTML/HTMLAreaElement.h b/Libraries/LibWeb/HTML/HTMLAreaElement.h
index ab7d31f18e7..c3082cefe0f 100644
--- a/Libraries/LibWeb/HTML/HTMLAreaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLAreaElement.h
@@ -7,10 +7,8 @@
#pragma once
-#include <LibGfx/Path.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/HTMLHyperlinkElementUtils.h>
-#include <LibWebCommon/PixelUnits.h>
namespace Web::HTML {
@@ -33,8 +31,6 @@ public:
ShapeState shape_state() const;
Vector<double> shape_coords() const;
- Optional<Gfx::Path> shape_path(CSSPixelSize image_size) const;
-
private:
HTMLAreaElement(DOM::Document&, DOM::QualifiedName);
diff --git a/Libraries/LibWeb/Painting/BoxViews.cpp b/Libraries/LibWeb/Painting/BoxViews.cpp
index c0eb6fd4851..4fcbf5cc38f 100644
--- a/Libraries/LibWeb/Painting/BoxViews.cpp
+++ b/Libraries/LibWeb/Painting/BoxViews.cpp
@@ -642,7 +642,7 @@ Layout::RustFFI::FfiFocusedTextControlSelection resolve_focused_text_control_sel
return selection;
}
-Layout::RustFFI::FfiFocusedAreaOutline resolve_focused_area_outline(Layout::BegunRead const& read, DOM::Document const& document, Vector<u8>& path_bytes)
+Layout::RustFFI::FfiFocusedAreaOutline resolve_focused_area_outline(Layout::BegunRead const& read, DOM::Document const& document)
{
// https://html.spec.whatwg.org/multipage/interaction.html#focusable-area
// The shapes of area elements in an image map associated with an img element that is being rendered and is not
@@ -664,13 +664,8 @@ Layout::RustFFI::FfiFocusedAreaOutline resolve_focused_area_outline(Layout::Begu
auto area_computed_values = area_element->computed_style();
if (!area_computed_values || area_computed_values->outline_style() != CSS::OutlineStyle::Auto)
return outline;
- auto path = area_element->shape_path(absolute_rect(*layout_node).size());
- if (!path.has_value())
- return outline;
- path_bytes = path->serialize_to_bytes();
outline.image = committed_row_slot(*layout_node);
- outline.path_bytes = path_bytes.data();
- outline.path_byte_count = path_bytes.size();
+ outline.area = DOM::NodeIdentity::of(*area_element).style_node().value();
return outline;
}
diff --git a/Libraries/LibWeb/Painting/BoxViews.h b/Libraries/LibWeb/Painting/BoxViews.h
index 2b4ee11376b..27d6c67c10a 100644
--- a/Libraries/LibWeb/Painting/BoxViews.h
+++ b/Libraries/LibWeb/Painting/BoxViews.h
@@ -84,7 +84,7 @@ WEB_API CSSPixelRect caret_rect_for_child_offset(Layout::BegunRead const&, Layou
// Per-document paint facts the recording inputs carry, resolved once per recording.
WEB_API Layout::RustFFI::FfiCaretPaint resolve_document_caret_paint(Layout::BegunRead const&, DOM::Document&);
WEB_API Layout::RustFFI::FfiFocusedTextControlSelection resolve_focused_text_control_selection(Layout::BegunRead const&, DOM::Document const&);
-WEB_API Layout::RustFFI::FfiFocusedAreaOutline resolve_focused_area_outline(Layout::BegunRead const&, DOM::Document const&, Vector<u8>& path_bytes);
+WEB_API Layout::RustFFI::FfiFocusedAreaOutline resolve_focused_area_outline(Layout::BegunRead const&, DOM::Document const&);
WEB_API void push_highlight_pseudo_styles(DOM::Element const&);
// The node a layout row stands for, as the arena names it.
diff --git a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
index 2b5f87b096a..4dbeee43811 100644
--- a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
+++ b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
@@ -14,7 +14,6 @@
#include <LibGfx/CornerRadii.h>
#include <LibGfx/GradientInterpolation.h>
#include <LibGfx/Matrix4x4.h>
-#include <LibGfx/Path.h>
#include <LibGfx/TextLayout.h>
#include <LibWeb/Animations/DocumentTimeline.h>
#include <LibWeb/CSS/Enums.h>
@@ -630,8 +629,7 @@ Optional<DisplayListRecording> start_rust_display_list_recording(Layout::BegunRe
}
inputs.caret = resolve_document_caret_paint(read, document);
inputs.focused_text_control = resolve_focused_text_control_selection(read, document);
- Vector<u8> focused_area_path_bytes;
- inputs.focused_area_outline = resolve_focused_area_outline(read, document, focused_area_path_bytes);
+ inputs.focused_area_outline = resolve_focused_area_outline(read, document);
{
auto color_scheme = document.canvas_color_scheme(read);
bool opaque_canvas = false;
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index bd2eaa4a27b..8429b118e75 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -4395,14 +4395,15 @@ impl LayoutNodeArena {
self.shape_writes.identity() + self.bound_rows.borrow().version()
}
- /// The text rows and the replaced, layer image and SVG paint resource tables as they are now,
- /// for a recording to read. Each is shared until the arena next writes it.
+ /// The text rows, the replaced, layer image and SVG paint resource tables, and the image maps as they are now, for
+ /// a recording to read. Each is shared until the arena next writes it.
pub(crate) fn publish_paint_facts(&mut self) -> crate::painting::published_frame::PublishedPaintFacts {
crate::painting::published_frame::PublishedPaintFacts {
text: self.text_slots.published.publish(),
replaced: self.replaced_paint_facts.borrow().clone(),
layer_images: self.layer_image_paint_facts.borrow().clone(),
svg_paint_resources: self.svg_paint_resources.publish(),
+ image_maps: self.image_map_areas().snapshot(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/host/paint.rs b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
index 221b4518f0d..335fa94ff43 100644
--- a/Libraries/LibWeb/Rust/src/painting/host/paint.rs
+++ b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
@@ -95,14 +95,10 @@ impl FfiRecordingInputs {
};
// SAFETY: The caller lends these arrays and buffers for this call.
- let (grid_overlays, flex_overlays, outline_path, text_fragment_indications) = unsafe {
+ let (grid_overlays, flex_overlays, text_fragment_indications) = unsafe {
(
ak::slice_from_raw_parts(self.grid_overlays, self.grid_overlay_count),
ak::slice_from_raw_parts(self.flex_overlays, self.flex_overlay_count),
- ak::slice_from_raw_parts(
- self.focused_area_outline.path_bytes,
- self.focused_area_outline.path_byte_count,
- ),
ak::slice_from_raw_parts(self.text_fragment_indications, self.text_fragment_indication_count),
)
};
@@ -205,9 +201,9 @@ impl FfiRecordingInputs {
start: control.start,
end: control.end,
}),
- focused_area_outline: (!outline_path.is_empty()).then(|| FocusedAreaOutline {
+ focused_area_outline: (self.focused_area_outline.area != 0).then_some(FocusedAreaOutline {
image: self.focused_area_outline.image,
- path_bytes: outline_path.into(),
+ area: self.focused_area_outline.area,
}),
text_fragment_indications: text_fragment_indications.to_vec().into(),
mark_color: self.mark_color,
@@ -252,14 +248,13 @@ pub struct FfiFocusedTextControlSelection {
}
/// The focus ring of a focused image-map area, painted by the image whose rendering makes the
-/// area's shape focusable. No path bytes means no focus ring.
+/// area's shape focusable, along the shape the image's map gives the area its style-tree identity
+/// names. An identity of zero means no focus ring.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiFocusedAreaOutline {
pub image: crate::layout::node_data::NodeSlotId,
- /// A serialised `Gfx::Path` in the image's own coordinate space, live for the recording call.
- pub path_bytes: *const u8,
- pub path_byte_count: usize,
+ pub area: u32,
}
/// The platform default font at an overlay label's CSS size and at that size in device pixels.
diff --git a/Libraries/LibWeb/Rust/src/painting/image_map_areas.rs b/Libraries/LibWeb/Rust/src/painting/image_map_areas.rs
index d08144e21d5..bd7873ec63e 100644
--- a/Libraries/LibWeb/Rust/src/painting/image_map_areas.rs
+++ b/Libraries/LibWeb/Rust/src/painting/image_map_areas.rs
@@ -158,17 +158,21 @@ fn shape_path(shape: AreaShape, coords: &[f64]) -> Option<Path> {
// corner is given by the coordinate (x2, y2), those coordinates being interpreted as CSS pixels from the
// top left corner of the image.
let [left, top, right, bottom] = corners.map(|corner| corner as f32);
- let mut builder = Path::new();
- builder.move_to(FloatPoint { x: left, y: top });
- builder.line_to(FloatPoint { x: right, y: top });
- builder.line_to(FloatPoint { x: right, y: bottom });
- builder.line_to(FloatPoint { x: left, y: bottom });
- builder.close();
- Some(builder)
+ Some(rectangle_path(left, top, right, bottom))
}
}
}
+fn rectangle_path(left: f32, top: f32, right: f32, bottom: f32) -> Path {
+ let mut builder = Path::new();
+ builder.move_to(FloatPoint { x: left, y: top });
+ builder.line_to(FloatPoint { x: right, y: top });
+ builder.line_to(FloatPoint { x: right, y: bottom });
+ builder.line_to(FloatPoint { x: left, y: bottom });
+ builder.close();
+ builder
+}
+
// The `<area>` elements of the image map an image is associated with, as the hit test sees them.
// The association and the areas themselves still live on the DOM, and this column is the copy a
// hit test can read without asking for either. The document republishes it before the next hit test
@@ -226,6 +230,20 @@ impl ImageMapAreaColumn {
}
}
+/// The shape of the area of the map of the image whose paintable row is `slot` that its style-tree identity `area` names,
+/// in the coordinate space of the image, whose size is `width` by `height`. None when the map lists no such area, or its
+/// shape is empty.
+pub(crate) fn area_shape_path(maps: &ImageMaps, slot: NodeSlotId, area: u32, width: f32, height: f32) -> Option<Path> {
+ let area = maps.get(&slot)?.iter().find(|published| published.style_node == area)?;
+ match &area.coverage {
+ // https://html.spec.whatwg.org/multipage/image-maps.html#image-map-processing-model
+ // The shape is a rectangle that exactly covers the entire image.
+ AreaCoverage::WholeImage => Some(rectangle_path(0.0, 0.0, width, height)),
+ AreaCoverage::Shape(path) => Some(path.clone()),
+ AreaCoverage::Nothing => None,
+ }
+}
+
/// The first area of the map of the image whose paintable row is `slot`, in tree order, whose shape covers the point,
/// named by its style-tree identity. Zero when the image has no map, or no shape covers the point.
pub(crate) fn area_for_point(maps: &ImageMaps, slot: NodeSlotId, x: f32, y: f32) -> u32 {
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_read.rs b/Libraries/LibWeb/Rust/src/painting/paint_read.rs
index 2f0330f01da..837429d8446 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_read.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_read.rs
@@ -480,6 +480,24 @@ impl<'a> PaintSource<'a> {
self.frame.expect("only a recording reads a frame")
}
+ /// The shape of an `<area>` of the map of the image whose paintable row is `image` (see
+ /// [`crate::painting::image_map_areas::area_shape_path`]).
+ pub(crate) fn image_map_area_shape_path(
+ &self,
+ image: NodeSlotId,
+ area: u32,
+ width: f32,
+ height: f32,
+ ) -> Option<libgfx_rust::path::Path> {
+ crate::painting::image_map_areas::area_shape_path(
+ &self.rows().paint_facts.image_maps,
+ image,
+ area,
+ width,
+ height,
+ )
+ }
+
fn rows(&self) -> &'a PublishedRows {
self.rows
}
diff --git a/Libraries/LibWeb/Rust/src/painting/published_frame.rs b/Libraries/LibWeb/Rust/src/painting/published_frame.rs
index ad8d3563934..d07884876c5 100644
--- a/Libraries/LibWeb/Rust/src/painting/published_frame.rs
+++ b/Libraries/LibWeb/Rust/src/painting/published_frame.rs
@@ -43,6 +43,7 @@ pub(crate) struct PublishedPaintFacts {
pub(crate) replaced: Arc<ReplacedPaintFactsTable>,
pub(crate) layer_images: Arc<LayerImagePaintFactsTable>,
pub(crate) svg_paint_resources: Arc<SvgPaintResourceRows>,
+ pub(crate) image_maps: Arc<crate::painting::image_map_areas::ImageMaps>,
}
/// One published generation of a document's layout tree shape and paintable rows, and of the
diff --git a/Libraries/LibWeb/Rust/src/painting/record/inputs.rs b/Libraries/LibWeb/Rust/src/painting/record/inputs.rs
index 9407cee0816..df66947cbc5 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/inputs.rs
@@ -131,7 +131,8 @@ pub(crate) struct FocusedTextControlSelection {
#[derive(Clone)]
pub(crate) struct FocusedAreaOutline {
pub image: NodeSlotId,
- pub path_bytes: Box<[u8]>,
+ /// The style-tree identity of the area.
+ pub area: u32,
}
#[derive(Clone)]
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
index 52bc50a250f..8b00a136596 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
@@ -151,9 +151,16 @@ fn paint_focused_area_outline<O: Observer>(recorder: &mut PaintRecorder<'_, O>,
if outline.image != paintable {
return;
}
- let path = libgfx_rust::path::Path::from_serialized_bytes(&outline.path_bytes);
- let converter = recorder.converter;
let image_rect = paintable_geometry::absolute_rect(recorder.source, paintable);
+ let Some(path) = recorder.source.image_map_area_shape_path(
+ outline.image,
+ outline.area,
+ image_rect.width.to_float(),
+ image_rect.height.to_float(),
+ ) else {
+ return;
+ };
+ let converter = recorder.converter;
let scale = recorder.inputs.device_pixels_per_css_pixel as f32;
let color = recorder.inputs.outline_auto_color;
let device_origin = converter.rounded_device_point(image_rect.location());
diff --git a/Tests/LibWeb/Ref/expected/image-map-default-area-focus-outline-ref.html b/Tests/LibWeb/Ref/expected/image-map-default-area-focus-outline-ref.html
new file mode 100644
index 00000000000..311c29520a6
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/image-map-default-area-focus-outline-ref.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<map name="m">
+ <area shape="default" href="#target" alt="target">
+</map>
+<img usemap="#m" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" width="200" height="100" alt="image map">
diff --git a/Tests/LibWeb/Ref/input/image-map-default-area-focus-outline.html b/Tests/LibWeb/Ref/input/image-map-default-area-focus-outline.html
new file mode 100644
index 00000000000..6c735a6f315
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/image-map-default-area-focus-outline.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<link rel="mismatch" href="../expected/image-map-default-area-focus-outline-ref.html" />
+<map name="m">
+ <area shape="default" href="#target" alt="target">
+</map>
+<img usemap="#m" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" width="200" height="100" alt="image map">
+<script>
+ document.querySelector("area").focus();
+</script>