Skip to content

Truly independent web browser

LibWeb: Expose the geometry painters use outside the border box

Aliaksandr Kalenikc34e9ee65d29
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs
index da4843ccbcf..711b6e1352a 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs
@@ -8,7 +8,7 @@ use crate::css::style_value::BorderImageSlicePayload;
use crate::painting::paint_read::PaintRead;
use crate::painting::record::trace::Observer;
-use crate::css::computed_value_views::LengthPercentageRef;
+use crate::css::computed_value_views::{ComputedValuesView, LengthPercentageRef};
use crate::css::css_enums::border_image_repeat;
use crate::css::css_pixels::{CssPixelFraction, CssPixels};
use crate::css::css_pixels::{CssPixelRect, CssPixelSize};
@@ -517,21 +517,32 @@ fn sides_present(present_edges: u8) -> [bool; 4] {
[PIECE_EDGE_TOP, PIECE_EDGE_RIGHT, PIECE_EDGE_BOTTOM, PIECE_EDGE_LEFT].map(|edge| present_edges & edge != 0)
}
-// https://drafts.csswg.org/css-backgrounds-3/#border-image-area
-fn border_image_area(
- outset_items: &[&StyleValueData],
- border_box_rect: CssPixelRect,
- border_width: [CssPixels; 4],
+pub(crate) fn border_image_outsets(
+ style: ComputedValuesView<'_>,
+ border_widths: [CssPixels; 4],
present_edges: u8,
-) -> CssPixelRect {
+) -> [CssPixels; 4] {
+ let outset_value = style_queries::handle_value(&style.border().border_image_outset)
+ .expect("computed border-image-outset lost its style value");
+ let outset_items = component_items(outset_value);
let side_is_present = sides_present(present_edges);
- let resolved_outset: [CssPixels; 4] = std::array::from_fn(|side| {
+ std::array::from_fn(|side| {
if side_is_present[side] {
- resolve_outset_px(outset_items[side % outset_items.len()], border_width[side])
+ resolve_outset_px(outset_items[side % outset_items.len()], border_widths[side])
} else {
CssPixels::default()
}
- });
+ })
+}
+
+// https://drafts.csswg.org/css-backgrounds-3/#border-image-area
+fn border_image_area(
+ style: ComputedValuesView<'_>,
+ border_box_rect: CssPixelRect,
+ border_width: [CssPixels; 4],
+ present_edges: u8,
+) -> CssPixelRect {
+ let resolved_outset = border_image_outsets(style, border_width, present_edges);
let mut border_image_rect = border_box_rect;
border_image_rect.inflate(
resolved_outset[0],
@@ -666,8 +677,6 @@ pub(crate) fn paint_border_image<O: Observer>(
};
let width_value = style_queries::handle_value(&border.border_image_width)
.expect("computed border-image-width lost its style value");
- let outset_value = style_queries::handle_value(&border.border_image_outset)
- .expect("computed border-image-outset lost its style value");
let repeat_value = style_queries::handle_value(&border.border_image_repeat)
.expect("computed border-image-repeat lost its style value");
let repeat_items = component_items(repeat_value);
@@ -680,12 +689,7 @@ pub(crate) fn paint_border_image<O: Observer>(
let repeat_x = repeat_keyword(repeat_items[0]);
let repeat_y = repeat_keyword(repeat_items[1 % repeat_items.len()]);
- let border_image_rect = border_image_area(
- &component_items(outset_value),
- border_box_rect,
- css_border_widths,
- present_edges,
- );
+ let border_image_rect = border_image_area(style, border_box_rect, css_border_widths, present_edges);
let (content, source_size) = if is_gradient {
// https://drafts.csswg.org/css-backgrounds-3/#border-image-slice
// Gradients have no natural size, so their concrete size is the border image area.
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/column_rule.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/column_rule.rs
index a2f6951fee8..105296f75c0 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/column_rule.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/column_rule.rs
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use crate::painting::paint_read::PaintRead;
+use crate::painting::paint_read::{GeometryRead, PaintRead};
use crate::painting::record::trace::Observer;
use crate::css::computed_value_views::ComputedValuesView;
@@ -28,12 +28,7 @@ pub(crate) fn paint_column_rules<O: Observer>(recorder: &mut PaintRecorder<'_, O
let Some(style) = recorder.source.node_style_if_live(paintable) else {
return;
};
- let Some(data) = paintable_geometry::committed_multicol_layout_data(recorder.source, paintable) else {
- return;
- };
- let content_box = paintable_geometry::absolute_rect(recorder.source, paintable);
let rule_width = style.column_rule_width();
- let is_right_to_left = style.direction() == direction::RTL;
let converter = recorder.converter;
let borders_data = BordersDataDevicePixels {
left: border_side(
@@ -50,21 +45,39 @@ pub(crate) fn paint_column_rules<O: Observer>(recorder: &mut PaintRecorder<'_, O
force_dark_role: ForceDarkRole::Border,
..BordersDataDevicePixels::default()
};
+ for rule_rect in column_rule_rects(recorder.source, paintable, style) {
+ paint_all_borders(
+ &mut recorder.recorder,
+ converter.rounded_device_rect(rule_rect),
+ CornerRadii::default(),
+ &borders_data,
+ );
+ }
+}
+
+/// The rules between the columns of each row of a multi-column container, in absolute CSS pixels.
+pub(crate) fn column_rule_rects(
+ arena: &impl GeometryRead,
+ paintable: NodeSlotId,
+ style: ComputedValuesView<'_>,
+) -> Vec<CssPixelRect> {
+ let Some(data) = paintable_geometry::committed_multicol_layout_data(arena, paintable) else {
+ return Vec::new();
+ };
+ let content_box = paintable_geometry::absolute_rect(arena, paintable);
+ let rule_width = style.column_rule_width();
+ let is_right_to_left = style.direction() == direction::RTL;
+ let mut rects = Vec::new();
for row in &data.rows {
for gap_index in 0..row.column_count.saturating_sub(1) {
let gap_center = data.geometry.gap_center(gap_index, content_box.width, is_right_to_left);
- let rule_rect = CssPixelRect::new(
+ rects.push(CssPixelRect::new(
content_box.x + gap_center - rule_width / 2usize,
content_box.y + row.block_offset,
rule_width,
row.block_size,
- );
- paint_all_borders(
- &mut recorder.recorder,
- converter.rounded_device_rect(rule_rect),
- CornerRadii::default(),
- &borders_data,
- );
+ ));
}
}
+ rects
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs
index 3327899168f..95708879ed8 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs
@@ -231,8 +231,13 @@ fn paint_pieces<O: Observer>(
if phase == PaintPhase::Outline && facts.is_visible {
let node = paintable;
- let outline = outline::outline_data_for_paint(recorder, node);
- let outline_offset = crate::painting::style_queries::outline_offset(recorder.source, node);
+ let (outline, outline_offset) = outline::outline_data_and_offset(
+ recorder.source,
+ node,
+ recorder.inputs.accessibility_focus_target,
+ || recorder.window_is_focused(),
+ recorder.inputs.outline_auto_color.0,
+ );
for piece_index in piece_indices {
let piece = &root_pieces[*piece_index as usize];
if piece.is_geometry_only_placeholder {
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
index 93e304ca764..d3b763c861f 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use crate::painting::paint_read::PaintRead;
use crate::painting::record::trace::Observer;
use crate::css::css_pixels::CssPixels;
@@ -19,9 +20,14 @@ use libgfx_rust::{CapStyle, Color, JoinStyle, ShouldAntiAlias};
pub(crate) fn paint_outline_phase<O: Observer>(recorder: &mut PaintRecorder<'_, O>, paintable: NodeSlotId) {
let node = paintable;
- let outline = outline_data_for_paint(recorder, node);
+ let (outline, outline_offset) = outline_data_and_offset(
+ recorder.source,
+ node,
+ recorder.inputs.accessibility_focus_target,
+ || recorder.window_is_focused(),
+ recorder.inputs.outline_auto_color.0,
+ );
if outline.is_some() {
- let outline_offset = crate::painting::style_queries::outline_offset(recorder.source, node);
for (border_box_rect, border_radii) in recorder.border_boxes_of_fragments(paintable) {
paint_outline(recorder, outline, outline_offset, border_box_rect, border_radii);
}
@@ -31,21 +37,19 @@ pub(crate) fn paint_outline_phase<O: Observer>(recorder: &mut PaintRecorder<'_,
// The assistive-technology focus target gets an auto-style ring in place of whatever outline its style computes —
// so the ring shows on an element with outline: none, and replaces an author outline rather than stacking on it.
-pub(crate) fn outline_data_for_paint<O: Observer>(
- recorder: &PaintRecorder<'_, O>,
+pub(crate) fn outline_data_and_offset(
+ arena: &impl PaintRead,
node: NodeSlotId,
-) -> Option<crate::painting::style_queries::OutlineData> {
- if Some(node) == recorder.inputs.accessibility_focus_target {
- return Some(crate::painting::style_queries::auto_outline_data(
- recorder.inputs.outline_auto_color.0,
- ));
- }
- crate::painting::style_queries::outline_data(
- recorder.source,
- node,
- || recorder.window_is_focused(),
- recorder.inputs.outline_auto_color.0,
- )
+ accessibility_focus_target: Option<NodeSlotId>,
+ window_is_focused: impl FnOnce() -> bool,
+ auto_outline_color: u32,
+) -> (Option<crate::painting::style_queries::OutlineData>, CssPixels) {
+ let outline = if Some(node) == accessibility_focus_target {
+ Some(crate::painting::style_queries::auto_outline_data(auto_outline_color))
+ } else {
+ crate::painting::style_queries::outline_data(arena, node, window_is_focused, auto_outline_color)
+ };
+ (outline, crate::painting::style_queries::outline_offset(arena, node))
}
pub(crate) fn outline_border_geometry(
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/shadow.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/shadow.rs
index 07371f0502e..62bcf48bf9b 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/shadow.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/shadow.rs
@@ -70,6 +70,43 @@ pub(crate) fn paint_box_shadow<O: Observer>(
);
}
+pub(crate) struct BoxShadowLayerGeometry {
+ pub offset_x: CssPixels,
+ pub offset_y: CssPixels,
+ pub blur_radius: CssPixels,
+ pub spread_distance: CssPixels,
+ pub content_rect: CssPixelRect,
+}
+
+pub(crate) fn box_shadow_layer_geometry(
+ layer: &crate::css::computed_value_types::ComputedShadow,
+ bordered_content_rect: CssPixelRect,
+ borderless_content_rect: CssPixelRect,
+) -> BoxShadowLayerGeometry {
+ BoxShadowLayerGeometry {
+ offset_x: CssPixels::from_raw(layer.offset_x),
+ offset_y: CssPixels::from_raw(layer.offset_y),
+ blur_radius: CssPixels::from_raw(layer.blur_radius),
+ spread_distance: CssPixels::from_raw(layer.spread_distance),
+ content_rect: if layer.is_inner() {
+ borderless_content_rect
+ } else {
+ bordered_content_rect
+ },
+ }
+}
+
+pub(crate) fn outer_box_shadow_rect(geometry: &BoxShadowLayerGeometry, device_pixel_ratio: f64) -> IntRect {
+ let converter = crate::painting::display_list::device_pixels::DevicePixelConverter::new(device_pixel_ratio);
+ let device_content_rect = converter.rounded_device_rect(geometry.content_rect);
+ let spread_distance = converter.rounded_device_pixels(geometry.spread_distance);
+ let mut shadow_rect =
+ device_content_rect.inflated_edges(spread_distance, spread_distance, spread_distance, spread_distance);
+ shadow_rect.x += converter.rounded_device_pixels(geometry.offset_x);
+ shadow_rect.y += converter.rounded_device_pixels(geometry.offset_y);
+ shadow_rect
+}
+
fn paint_box_shadow_layers<O: Observer>(
recorder: &mut PaintRecorder<'_, O>,
paintable: NodeSlotId,
@@ -87,17 +124,13 @@ fn paint_box_shadow_layers<O: Observer>(
// light glow — as Blink's ResolveShadowColor does, passing DarkModeFilter::ElementRole::kBackground.
// Box-shadow layers are ordered front-to-back, so we paint them in reverse.
for layer in layers.iter().rev() {
- let offset_x = converter.rounded_device_pixels(CssPixels::from_raw(layer.offset_x));
- let offset_y = converter.rounded_device_pixels(CssPixels::from_raw(layer.offset_y));
- let blur_radius = converter.rounded_device_pixels(CssPixels::from_raw(layer.blur_radius));
- let spread_distance = converter.rounded_device_pixels(CssPixels::from_raw(layer.spread_distance));
+ let geometry = box_shadow_layer_geometry(layer, bordered_content_rect, borderless_content_rect);
+ let offset_x = converter.rounded_device_pixels(geometry.offset_x);
+ let offset_y = converter.rounded_device_pixels(geometry.offset_y);
+ let blur_radius = converter.rounded_device_pixels(geometry.blur_radius);
+ let spread_distance = converter.rounded_device_pixels(geometry.spread_distance);
let inner = layer.is_inner();
-
- let device_content_rect = if inner {
- converter.rounded_device_rect(borderless_content_rect)
- } else {
- converter.rounded_device_rect(bordered_content_rect)
- };
+ let device_content_rect = converter.rounded_device_rect(geometry.content_rect);
if let Some(shadow_shapes) = &shadow_shapes {
recorder.recorder.paint_path_box_shadow(
@@ -171,10 +204,7 @@ fn paint_box_shadow_layers<O: Observer>(
ForceDarkRole::Background,
);
} else {
- let mut shadow_rect =
- device_content_rect.inflated_edges(spread_distance, spread_distance, spread_distance, spread_distance);
- shadow_rect.x += offset_x;
- shadow_rect.y += offset_y;
+ let shadow_rect = outer_box_shadow_rect(&geometry, recorder.inputs.device_pixels_per_css_pixel);
let mut shadow_corner_radii = corner_radii;
adjust_corners_for_spread_distance(&mut shadow_corner_radii, spread_distance);
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/table_backgrounds.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/table_backgrounds.rs
index ac22490eb32..5c9d62b6794 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/table_backgrounds.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/table_backgrounds.rs
@@ -76,7 +76,7 @@ pub(crate) fn paint_table_part_background<O: Observer>(recorder: &mut PaintRecor
}
/// The cells that originate in a table row, row group, column or column group, in paint order.
-fn originating_cells(arena: &impl PaintRead, part: NodeSlotId) -> Vec<NodeSlotId> {
+pub(crate) fn originating_cells(arena: &impl PaintRead, part: NodeSlotId) -> Vec<NodeSlotId> {
let mut cells = Vec::new();
let display = style_queries::display(arena, part);
if display.is_table_row() {
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/table_borders.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/table_borders.rs
index 5a21804abe0..727f7271251 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/table_borders.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/table_borders.rs
@@ -4,10 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use crate::painting::paint_read::GeometryRead;
use crate::painting::record::trace::Observer;
use crate::css::css_enums::line_style;
-use crate::css::css_pixels::CssPixels;
+use crate::css::css_pixels::{CssPixelRect, CssPixels};
use crate::layout::node_data::NodeSlotId;
use crate::layout::table_formatting_context;
use crate::layout::used_values::{collapsed_border_part_after_line, collapsed_border_part_before_line};
@@ -184,6 +185,38 @@ fn paint_edge<O: Observer>(recorder: &mut PaintRecorder<'_, O>, rect: IntRect, e
recorder.recorder.fill_rect(rect, edge.color, ForceDarkRole::Border);
}
+pub(crate) fn collapsed_table_borders_bounds_and_grid_lines(
+ arena: &impl GeometryRead,
+ table_paintable: NodeSlotId,
+) -> Option<(CssPixelRect, Vec<CssPixels>, Vec<CssPixels>)> {
+ let borders = crate::painting::paintable_geometry::committed_collapsed_table_borders(arena, table_paintable)?;
+ if borders.column_offsets.len() < 2 || borders.row_offsets.len() < 2 {
+ return None;
+ }
+ // The grid lines, in CSS pixels: each border is centered on its grid line and its rectangle is only snapped to
+ // device pixels once its extent is known (see device_rect).
+ let origin = crate::painting::paintable_geometry::absolute_rect(arena, table_paintable).location();
+ let xs: Vec<CssPixels> = borders.column_offsets.iter().map(|offset| origin.x + *offset).collect();
+ let ys: Vec<CssPixels> = borders.row_offsets.iter().map(|offset| origin.y + *offset).collect();
+ let widest_border = borders
+ .horizontal_edges
+ .iter()
+ .chain(&borders.vertical_edges)
+ .map(|edge| edge.border_data.width)
+ .max()
+ .unwrap_or_default();
+ // Dotted and dashed borders stroke along the edge of their rect, so allow more than the half-width on each
+ // side of a grid line that a filled border occupies.
+ let reach = widest_border + widest_border;
+ let bounds = CssPixelRect::new(
+ xs[0] - reach,
+ ys[0] - reach,
+ xs[xs.len() - 1] - xs[0] + reach + reach,
+ ys[ys.len() - 1] - ys[0] + reach + reach,
+ );
+ Some((bounds, xs, ys))
+}
+
pub(crate) fn paint_table_borders<O: Observer>(recorder: &mut PaintRecorder<'_, O>, table_paintable: NodeSlotId) {
// Painting according to the collapsing border model:
// https://www.w3.org/TR/CSS22/tables.html#collapsing-borders
@@ -204,11 +237,9 @@ pub(crate) fn paint_table_borders<O: Observer>(recorder: &mut PaintRecorder<'_,
debug_assert_eq!(borders.horizontal_edges.len(), (rows + 1) * columns);
debug_assert_eq!(borders.vertical_edges.len(), (columns + 1) * rows);
- // The grid lines, in CSS pixels: each border is centered on its grid line and its rectangle is only snapped to
- // device pixels once its extent is known (see device_rect).
- let origin = crate::painting::paintable_geometry::absolute_rect(recorder.source, table_paintable).location();
- let xs: Vec<CssPixels> = borders.column_offsets.iter().map(|offset| origin.x + *offset).collect();
- let ys: Vec<CssPixels> = borders.row_offsets.iter().map(|offset| origin.y + *offset).collect();
+ let Some((_, xs, ys)) = collapsed_table_borders_bounds_and_grid_lines(recorder.source, table_paintable) else {
+ return;
+ };
let horizontal_edges: Vec<Edge> = borders.horizontal_edges.iter().map(|edge| Edge::from(*edge)).collect();
let vertical_edges: Vec<Edge> = borders.vertical_edges.iter().map(|edge| Edge::from(*edge)).collect();
let no_edge = Edge::default();
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
index 2d3f1394ad8..ae06e1dcf32 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
@@ -661,6 +661,45 @@ fn highlight_rects<O: Observer>(
text_fragment::rects_for_selection_offsets(recorder.source, fragment, offsets)
}
+pub(crate) fn text_shadow_layers_and_bounds(
+ shadow_layers: &[ShadowLayer],
+ fragment_device_rect: IntRect,
+ scale: f64,
+) -> (Vec<TextShadowLayer>, IntRect) {
+ let converter = crate::painting::display_list::device_pixels::DevicePixelConverter::new(scale);
+ let mut shadows_bounding_rect = IntRect::default();
+ let layers: Vec<TextShadowLayer> = shadow_layers
+ .iter()
+ .rev()
+ .map(|layer| {
+ let blur_radius = converter.rounded_device_pixels(layer.blur_radius);
+ let offset = FloatPoint {
+ x: layer.offset_x.to_float() * scale as f32,
+ y: layer.offset_y.to_float() * scale as f32,
+ };
+ let rounded_offset = IntPoint {
+ x: offset.x.round() as i32,
+ y: offset.y.round() as i32,
+ };
+ // Space around the painted text to allow it to blur.
+ let margin = blur_radius * 2;
+ shadows_bounding_rect = shadows_bounding_rect.united(IntRect::new(
+ fragment_device_rect.x + rounded_offset.x - margin,
+ fragment_device_rect.y + rounded_offset.y - margin,
+ fragment_device_rect.width + margin * 2,
+ fragment_device_rect.height + margin * 2,
+ ));
+ TextShadowLayer {
+ offset,
+ rounded_offset,
+ blur_radius,
+ color: Color(layer.color),
+ }
+ })
+ .collect();
+ (layers, shadows_bounding_rect)
+}
+
fn paint_text_shadow<O: Observer>(
recorder: &mut PaintRecorder<'_, O>,
block: NodeSlotId,
@@ -716,36 +755,7 @@ fn paint_text_shadow<O: Observer>(
// filter text shadows either: DarkModeFilter::ApplyToFlagsIfNeeded recolors only the paint flags, never the
// shadow filter that TextShadowPainter puts on the text's layer.
// Shadow layers are ordered front-to-back, so we paint them in reverse.
- let mut shadows_bounding_rect = IntRect::default();
- let layers: Vec<TextShadowLayer> = shadow_layers
- .iter()
- .rev()
- .map(|layer| {
- let blur_radius = converter.rounded_device_pixels(layer.blur_radius);
- let offset = FloatPoint {
- x: layer.offset_x.to_float() * scale as f32,
- y: layer.offset_y.to_float() * scale as f32,
- };
- let rounded_offset = IntPoint {
- x: offset.x.round() as i32,
- y: offset.y.round() as i32,
- };
- // Space around the painted text to allow it to blur.
- let margin = blur_radius * 2;
- shadows_bounding_rect = shadows_bounding_rect.united(IntRect::new(
- fragment_device_rect.x + rounded_offset.x - margin,
- fragment_device_rect.y + rounded_offset.y - margin,
- fragment_device_rect.width + margin * 2,
- fragment_device_rect.height + margin * 2,
- ));
- TextShadowLayer {
- offset,
- rounded_offset,
- blur_radius,
- color: Color(layer.color),
- }
- })
- .collect();
+ let (layers, shadows_bounding_rect) = text_shadow_layers_and_bounds(shadow_layers, fragment_device_rect, scale);
recorder.recorder.paint_text_shadow(
&layers,
shadows_bounding_rect,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rs
index 29d202c3931..64a3f4c01a9 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rs
@@ -459,14 +459,28 @@ fn build_triangle_wave_path(from: IntPoint, to: IntPoint, amplitude: f32) -> Pat
path
}
-pub(crate) fn paint_decoration_lines<O: Observer>(
- recorder: &mut PaintRecorder<'_, O>,
- block: NodeSlotId,
- fragment_index: u32,
+pub(crate) enum DecorationStrokeGeometry {
+ Single(LineStyle),
+ Double { first_y: i32, second_y: i32 },
+ Wavy { center_y: i32, amplitude: i32 },
+}
+
+pub(crate) struct DecorationLineGeometry {
+ pub start: IntPoint,
+ pub end: IntPoint,
+ pub thickness: i32,
+ pub color: Color,
+ pub should_skip_ink: bool,
+ pub stroke: DecorationStrokeGeometry,
+}
+
+pub(crate) fn decoration_line_geometry(
fragment_box: CssPixelRect,
set: &TextDecorationSet,
-) {
- let converter = recorder.converter;
+ device_pixel_ratio: f64,
+) -> Vec<DecorationLineGeometry> {
+ let converter = crate::painting::display_list::device_pixels::DevicePixelConverter::new(device_pixel_ratio);
+ let mut lines = Vec::new();
let glyph_height = CssPixels::nearest_value_for_f32(set.decorating_font_pixel_size);
let baseline = set.baseline;
let two = CssPixels::from_integer(2);
@@ -509,7 +523,7 @@ pub(crate) fn paint_decoration_lines<O: Observer>(
// Compute the center Y of the decoration stroke. For underline and overline, offset by half the thickness
// so the near edge of the stroke aligns with the intended position.
let line_center_y = match line {
- text_decoration_line::NONE => return,
+ text_decoration_line::NONE => break,
text_decoration_line::UNDERLINE => {
// https://drafts.csswg.org/css-text-decor-4/#text-underline-position-property
// FIXME: Support text-decoration: underline on vertical text
@@ -540,7 +554,7 @@ pub(crate) fn paint_decoration_lines<O: Observer>(
baseline - point_size * CssPixels::nearest_value_for_f32(0.5)
}
// Blink: conforming user agents may simply not blink the text.
- _ => return,
+ _ => break,
};
let line_start_point = converter.rounded_device_point(
@@ -563,6 +577,66 @@ pub(crate) fn paint_decoration_lines<O: Observer>(
&& set.anchored_at_fragment_baseline
&& (line == text_decoration_line::UNDERLINE || line == text_decoration_line::OVERLINE);
+ let line_y = line_start_point.y;
+ let stroke = match line_style {
+ text_decoration_style::SOLID => DecorationStrokeGeometry::Single(LineStyle::Solid),
+ text_decoration_style::DASHED => DecorationStrokeGeometry::Single(LineStyle::Dashed),
+ text_decoration_style::DOTTED => DecorationStrokeGeometry::Single(LineStyle::Dotted),
+ text_decoration_style::DOUBLE => {
+ // Two parallel lines with a 1px gap, expanding away from the text.
+ let step = device_line_thickness + 1;
+ let mut first_y = line_y;
+ let mut second_y = line_y;
+ match line {
+ text_decoration_line::UNDERLINE => second_y += step,
+ text_decoration_line::OVERLINE => second_y -= step,
+ _ => {
+ first_y -= step / 2;
+ second_y = first_y + step;
+ }
+ }
+ DecorationStrokeGeometry::Double { first_y, second_y }
+ }
+ _ => {
+ // Wavy: the wave oscillates amplitude/2 above and below its center.
+ let amplitude = device_line_thickness * 3;
+ let mut wave_y = line_y;
+ match line {
+ text_decoration_line::UNDERLINE => wave_y += device_line_thickness / 2 + 1,
+ text_decoration_line::OVERLINE => wave_y -= device_line_thickness / 2 + 1,
+ _ => {}
+ }
+ DecorationStrokeGeometry::Wavy {
+ center_y: wave_y,
+ amplitude,
+ }
+ }
+ };
+ lines.push(DecorationLineGeometry {
+ start: line_start_point,
+ end: line_end_point,
+ thickness: device_line_thickness,
+ color: line_color,
+ should_skip_ink,
+ stroke,
+ });
+ }
+ lines
+}
+
+pub(crate) fn paint_decoration_lines<O: Observer>(
+ recorder: &mut PaintRecorder<'_, O>,
+ block: NodeSlotId,
+ fragment_index: u32,
+ fragment_box: CssPixelRect,
+ set: &TextDecorationSet,
+) {
+ for geometry in decoration_line_geometry(fragment_box, set, recorder.inputs.device_pixels_per_css_pixel) {
+ let line_start_point = geometry.start;
+ let line_end_point = geometry.end;
+ let device_line_thickness = geometry.thickness;
+ let line_color = geometry.color;
+ let should_skip_ink = geometry.should_skip_ink;
let segments = if !should_skip_ink {
vec![DecorationSegment {
start_x: line_start_point.x,
@@ -595,49 +669,22 @@ pub(crate) fn paint_decoration_lines<O: Observer>(
);
};
- match line_style {
- text_decoration_style::SOLID => {
+ match geometry.stroke {
+ DecorationStrokeGeometry::Single(style) => {
for segment in &segments {
- draw_line_for_segment(recorder, *segment, line_y, LineStyle::Solid);
+ draw_line_for_segment(recorder, *segment, line_y, style);
}
}
- text_decoration_style::DOUBLE => {
- // Two parallel lines with a 1px gap, expanding away from the text.
- let step = device_line_thickness + 1;
- let mut first_y = line_y;
- let mut second_y = line_y;
- match line {
- text_decoration_line::UNDERLINE => second_y += step,
- text_decoration_line::OVERLINE => second_y -= step,
- _ => {
- first_y -= step / 2;
- second_y = first_y + step;
- }
- }
+ DecorationStrokeGeometry::Double { first_y, second_y } => {
for segment in &segments {
draw_line_for_segment(recorder, *segment, first_y, LineStyle::Solid);
draw_line_for_segment(recorder, *segment, second_y, LineStyle::Solid);
}
}
- text_decoration_style::DASHED => {
- for segment in &segments {
- draw_line_for_segment(recorder, *segment, line_y, LineStyle::Dashed);
- }
- }
- text_decoration_style::DOTTED => {
- for segment in &segments {
- draw_line_for_segment(recorder, *segment, line_y, LineStyle::Dotted);
- }
- }
- _ => {
- // Wavy: the wave oscillates amplitude/2 above and below its center.
- let amplitude = device_line_thickness * 3;
- let mut wave_y = line_y;
- match line {
- text_decoration_line::UNDERLINE => wave_y += device_line_thickness / 2 + 1,
- text_decoration_line::OVERLINE => wave_y -= device_line_thickness / 2 + 1,
- _ => {}
- }
+ DecorationStrokeGeometry::Wavy {
+ center_y: wave_y,
+ amplitude,
+ } => {
for segment in &segments {
let from = IntPoint {
x: segment.start_x,