Truly independent web browser
LibWeb: Keep the glyph ink of each line with its inline content
Aliaksandr Kalenik0bd47f3be349
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rsindex fa04a3b8fed..039447e83f5 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs@@ -9,6 +9,7 @@ use super::geometry::{AvailableSize, Side};use super::layout_node_arena::LayoutNodeArena;use super::node_data::NodeSlotId;use super::{formatting_context, inline_formatting_context, used_values};+use crate::css::css_pixels::CssPixelRect;use crate::css::style::fast_hash::FastSet as HashSet;// Finalized inline output shared by committed fragments, formatting-context caches and painting.@@ -33,6 +34,22 @@ pub struct InlineContent {pub inline_boxes: InlineBoxIndex,/// The fragments of each node, by node.pub text_fragments: TextFragmentIndex,+ pub(crate) glyph_ink_of_lines: GlyphInkOfLines,+}++#[derive(Default)]+pub struct GlyphInkOfLines(std::sync::OnceLock<Box<[CssPixelRect]>>);++impl Clone for GlyphInkOfLines {+ fn clone(&self) -> Self {+ Self::default()+ }+}++impl PartialEq for GlyphInkOfLines {+ fn eq(&self, _: &Self) -> bool {+ true+ }}/// The indices of each node's fragments in its line root's content, which queries that start from a node look up in a@@ -515,7 +532,9 @@ impl InlineContent {inline_box_rects_after_clamp: data.inline_box_rects_after_clamp,elided_content_rects,writing_mode,+ glyph_ink_of_lines: GlyphInkOfLines::default(),};+ content.glyph_ink_of_lines();// Plain text needs no interleaving and keeps its single foreground producer.if content.inline_box_pieces.is_empty() && !content.fragments.iter().any(|fragment| fragment.is_atomic_inline) {return content;@@ -581,9 +600,40 @@ impl InlineContent {}combined.drop_repeated_placeholder_pieces();combined.index_inline_boxes();+ let translated_glyph_ink_of_lines = contents+ .iter()+ .map(|(content, _, offset)| {+ let ink = content.glyph_ink_of_lines.0.get()?;+ (content.writing_mode == crate::css::css_enums::writing_mode::HORIZONTAL_TB).then(|| {+ ink.iter()+ .map(|rect| rect.translated(offset.x, offset.y))+ .collect::<Vec<_>>()+ })+ })+ .collect::<Option<Vec<_>>>();+ if let Some(translated) = translated_glyph_ink_of_lines {+ let _ = combined+ .glyph_ink_of_lines+ .0+ .set(translated.into_iter().flatten().collect());+ }std::sync::Arc::new(combined)}+ pub(crate) fn glyph_ink_of_lines(&self) -> &[CssPixelRect] {+ self.glyph_ink_of_lines.0.get_or_init(|| {+ let mut ink = vec![CssPixelRect::default(); self.lines.len()];+ for fragment in &self.fragments {+ if let Some(line_ink) = ink.get_mut(fragment.line_index as usize) {+ line_ink.unite(crate::painting::text_fragment::glyph_ink_rect_in_containing_block(+ fragment,+ ));+ }+ }+ ink.into_boxed_slice()+ })+ }+/// Indexes the pieces of each inline box again, after they were renumbered or given to other boxes.pub(crate) fn index_inline_boxes(&mut self) {self.inline_boxes = InlineBoxIndex::of(&self.inline_box_pieces);@@ -883,7 +933,6 @@ impl<'content> PhysicalPlane<'content> {}pub(crate) fn delta(&self, delta: used_values::FfiCssPixelPoint) -> used_values::FfiCssPixelPoint {- use crate::css::css_pixels::CssPixelRect;let origin = self.frame.rect_to_physical(CssPixelRect::default(), self.content_size);let moved = self.frame.rect_to_physical(CssPixelRect::new(delta.x, delta.y, CssPixels::default(), CssPixels::default()),diff --git a/Libraries/LibWeb/Rust/src/painting/ink_overflow.rs b/Libraries/LibWeb/Rust/src/painting/ink_overflow.rsindex 89c70633ce7..6c213011384 100644--- a/Libraries/LibWeb/Rust/src/painting/ink_overflow.rs+++ b/Libraries/LibWeb/Rust/src/painting/ink_overflow.rs@@ -5,7 +5,7 @@*/use crate::css::computed_value_views::ComputedValuesView;-use crate::css::css_enums::{line_style, overflow, text_decoration_line, text_decoration_style, writing_mode};+use crate::css::css_enums::{line_style, overflow, text_decoration_line, text_decoration_style};use crate::css::css_pixels::{CssPixelRect, CssPixels};use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};use crate::painting::border_radii::BorderRadii;@@ -209,9 +209,40 @@ pub(crate) fn own_ink_overflow(}}let filter = fragment_ownership::effective_filter(source, owner);- filter.for_each_owned_fragment_index(side.fragments().len(), |index| {- include_text_fragment(source, paint_state, root, index as u32, &mut ink.rect);- });+ let owns_every_fragment = owner == root && filter.include_everything && filter.excluded.is_empty();+ let mut text_ink = CssPixelRect::default();+ if owns_every_fragment {+ for (line_index, line_ink) in side.glyph_ink_of_lines().iter().enumerate() {+ if !line_ink.is_empty() {+ text_ink.unite(line_ink.translated_by(positions.of_line(side.lines(), line_index)));+ }+ }+ for (index, fragment) in side.fragments().iter().enumerate() {+ if fragment.glyph_run.is_none() {+ continue;+ }+ let rect = text_fragment::rect_in_containing_block(fragment)+ .translated_by(positions.of_line(side.lines(), fragment.line_index as usize));+ include_text_paint_effects(source, paint_state, root, index as u32, rect, &mut text_ink);+ }+ if crate::painting::record::ink_bounds::verification().enabled {+ let mut text_ink_of_each_fragment = CssPixelRect::default();+ for index in 0..side.fragments().len() {+ include_text_fragment(source, paint_state, root, index as u32, &mut text_ink_of_each_fragment);+ }+ assert!(+ text_ink == text_ink_of_each_fragment+ || (text_ink.is_empty() && text_ink_of_each_fragment.is_empty())+ || (css_rect_is_saturated(text_ink) && css_rect_is_saturated(text_ink_of_each_fragment)),+ "The glyph ink kept with the lines of {root:?} is stale: {text_ink:?}, each fragment gives {text_ink_of_each_fragment:?}"+ );+ }+ } else {+ filter.for_each_owned_fragment_index(side.fragments().len(), |index| {+ include_text_fragment(source, paint_state, root, index as u32, &mut text_ink);+ });+ }+ ink.rect.unite(text_ink);}ink.unbounded |= css_rect_is_saturated(ink.rect);ink@@ -281,33 +312,38 @@ fn include_text_fragment() {let side = source.committed_side_data(root);let fragment = &side.fragments()[index as usize];- let Some(run) = &fragment.glyph_run else {+ if fragment.glyph_run.is_none() {return;- };+ }let rect = text_fragment::absolute_rect(source, fragment);- bounds.unite(rect);- let glyph = run.bounding_box;- let pixel = CssPixels::nearest_value_for_f32;- let glyph_rect = if fragment.writing_mode == writing_mode::HORIZONTAL_TB {- CssPixelRect::new(- rect.x + pixel(glyph.x),- rect.y + fragment.baseline + pixel(glyph.y),- pixel(glyph.width),- pixel(glyph.height),- )- } else {- CssPixelRect::new(- rect.right() - fragment.baseline - pixel(glyph.y + glyph.height),- rect.y + pixel(glyph.x),- pixel(glyph.height),- pixel(glyph.width),- )+ let rect_in_containing_block = text_fragment::rect_in_containing_block(fragment);+ bounds.unite(+ text_fragment::glyph_ink_rect_in_containing_block(fragment)+ .translated(rect.x - rect_in_containing_block.x, rect.y - rect_in_containing_block.y),+ );+ include_text_paint_effects(source, state, root, index, rect, bounds);+}++fn include_text_paint_effects(+ source: &impl PaintRead,+ state: &PublishedPaintState,+ root: NodeSlotId,+ index: u32,+ rect: CssPixelRect,+ bounds: &mut CssPixelRect,+) {+ let side = source.committed_side_data(root);+ let fragment = &side.fragments()[index as usize];+ let Some(run) = &fragment.glyph_run else {+ return;};- bounds.unite(glyph_rect);let Some(style) = source.node_style_if_live(fragment.style_source) else {return;};- bounds.unite(inflated(glyph_rect, text_stroke_reach(style)));+ bounds.unite(inflated(+ text_fragment::glyph_rect_of_fragment_at(fragment, run, rect),+ text_stroke_reach(style),+ ));let mut span = text::RenderSpan {fragment_index: index,start_code_unit: 0,@@ -372,14 +408,6 @@ fn include_text_fragment(for set in text_decoration::decoration_sets_for_span(source, root, &span) {bounds.unite(text_decoration::decoration_ink_bounds(rect, &set));}- bounds.unite(text_fragment::rect_for_selection_offsets(- source,- fragment,- text_fragment::SelectionOffsets {- start: 0,- end: fragment.length_in_code_units,- },- ));}fn has_resampled_ink(style: crate::layout::ComputedValuesView<'_>) -> bool {diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs b/Libraries/LibWeb/Rust/src/painting/paintable_data.rsindex 10f751c3190..c9af72766bc 100644--- a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs+++ b/Libraries/LibWeb/Rust/src/painting/paintable_data.rs@@ -266,6 +266,11 @@ impl CommittedSideData {pub(crate) fn fragments(&self) -> &[FragmentRecord] {self.inline_content.as_ref().map_or(&[], |content| &content.fragments)}+ pub(crate) fn glyph_ink_of_lines(&self) -> &[crate::css::css_pixels::CssPixelRect] {+ self.inline_content+ .as_ref()+ .map_or(&[], |content| content.glyph_ink_of_lines())+ }/// For a line root, the indices of the fragments of `node` among its fragments, in fragment order.pub(crate) fn fragment_indices_of(&self, node: NodeSlotId) -> &[u32] {self.inline_contentdiff --git a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs b/Libraries/LibWeb/Rust/src/painting/text_fragment.rsindex c79b5860ccf..0c6335ec19a 100644--- a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs+++ b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs@@ -282,9 +282,16 @@ pub(crate) fn rect_for_selection_offsets(layout_arena: &impl PaintRead,fragment: &FragmentRecord,offsets: SelectionOffsets,+) -> CssPixelRect {+ rect_for_selection_offsets_of_fragment_at(fragment, absolute_rect(layout_arena, fragment), offsets)+}++fn rect_for_selection_offsets_of_fragment_at(+ fragment: &FragmentRecord,+ mut rect: CssPixelRect,+ offsets: SelectionOffsets,) -> CssPixelRect {let horizontal = is_horizontal(fragment);- let mut rect = absolute_rect(layout_arena, fragment);let length = fragment.length_in_code_units;let start_in_text = offsets.start.min(length);@@ -341,6 +348,48 @@ pub(crate) fn rect_for_selection_offsets(rect}+pub(crate) fn glyph_rect_of_fragment_at(+ fragment: &FragmentRecord,+ run: &crate::layout::inline_content::GlyphRunRecord,+ rect: CssPixelRect,+) -> CssPixelRect {+ let glyph = run.bounding_box;+ let pixel = CssPixels::nearest_value_for_f32;+ if is_horizontal(fragment) {+ CssPixelRect::new(+ rect.x + pixel(glyph.x),+ rect.y + fragment.baseline + pixel(glyph.y),+ pixel(glyph.width),+ pixel(glyph.height),+ )+ } else {+ CssPixelRect::new(+ rect.right() - fragment.baseline - pixel(glyph.y + glyph.height),+ rect.y + pixel(glyph.x),+ pixel(glyph.height),+ pixel(glyph.width),+ )+ }+}++pub(crate) fn glyph_ink_rect_in_containing_block(fragment: &FragmentRecord) -> CssPixelRect {+ let Some(run) = &fragment.glyph_run else {+ return CssPixelRect::default();+ };+ let rect = rect_in_containing_block(fragment);+ let mut ink = rect;+ ink.unite(glyph_rect_of_fragment_at(fragment, run, rect));+ ink.unite(rect_for_selection_offsets_of_fragment_at(+ fragment,+ rect,+ SelectionOffsets {+ start: 0,+ end: fragment.length_in_code_units,+ },+ ));+ ink+}+/// Like `rect_for_selection_offsets()`, but with one rect for each visually separate piece of the range: Code units/// that are adjacent in the text can be apart on the line when the fragment mixes directions.pub(crate) fn rects_for_selection_offsets(