Truly independent web browser
LibWeb: Store trimmed whitespace advance and ascent on fragments
Aliaksandr Kalenikac608b3065eb
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rsindex 6850760d860..ab27c60f540 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs@@ -184,6 +184,9 @@ pub struct GlyphRunRecord {pub is_upright: bool,// Conservative painted bounds in CSS pixels, relative to the run's baseline origin.pub bounding_box: libgfx_rust::FloatRect,+ /// The font's ascent and descent in whole pixels, as layout counts them.+ pub ascent: CssPixels,+ pub descent: CssPixels,/// Built the first time a caret, selection or hit-testing query asks.pub cluster_geometry: std::sync::OnceLock<ClusterGeometry>,}@@ -313,26 +316,14 @@ impl ClusterGeometry {self.x_for_offset(length, false).unwrap_or_default()}- /// How far `x` lies past the end of the first `length` code units, in the direction their last cluster runs.- pub fn distance_past_end(&self, x: f32, length: usize) -> f32 {- let end_x = self.end_x(length);- let ends_right_to_left = self- .clusters- .iter()- .rfind(|cluster| cluster.start < length)- .is_some_and(|cluster| cluster.is_rtl);- if ends_right_to_left { end_x - x } else { x - end_x }- }-/// The offset for the inline coordinate `x` within the first `length` code units: the nearer edge of the grapheme- /// under `x`, the right one at its middle, or else the nearest grapheme edge. Also returns how far `x` is from the- /// text, zero within it.+ /// under `x`, the right one at its middle, or else the nearest grapheme edge.pub fn offset_for_x(&self,x: f32,length: usize,next_grapheme_boundary: impl Fn(usize) -> Option<usize>,- ) -> (usize, f32) {+ ) -> usize {let mut nearest = (0, f32::INFINITY);for cluster in self.clusters.iter().filter(|cluster| cluster.start < length) {let end = cluster.end.min(length);@@ -347,7 +338,7 @@ impl ClusterGeometry {std::mem::swap(&mut left, &mut right);}if x >= left.1 && x <= right.1 {- return (if x - left.1 < right.1 - x { left.0 } else { right.0 }, 0.0);+ return if x - left.1 < right.1 - x { left.0 } else { right.0 };}let edge = if x < left.1 {(left.0, left.1 - x)@@ -360,7 +351,7 @@ impl ClusterGeometry {grapheme_start = grapheme_end;}}- nearest+ nearest.0}/// The boundary at the physical left or right edge of the clusters that advance, and whether it ends its cluster.@@ -405,7 +396,8 @@ pub struct FragmentRecord {pub dom_start_offset_in_node: usize,pub dom_end_offset_in_node: usize,pub dom_end_offset_with_trailing_whitespace: usize,- pub trailing_whitespace_length_in_code_units: usize,+ /// The whitespace the line trimmed off the end of the fragment, which carets and selections still reach.+ pub(crate) trimmed_trailing_whitespace: super::line_box_fragment::TrailingWhitespace,pub glyph_run: Option<std::sync::Arc<GlyphRunRecord>>,}@@ -466,6 +458,8 @@ impl InlineContent {libgfx_rust::text_layout::glyph_run_bounding_box(&glyph_data.font, &glyph_data.glyphs, 1.0),);std::sync::Arc::new(GlyphRunRecord {+ ascent: super::line_builder::font_metric_in_whole_pixels(glyph_data.font.facts().ascent),+ descent: super::line_builder::font_metric_in_whole_pixels(glyph_data.font.facts().descent),glyphs: glyph_data.glyphs,font: glyph_data.font,is_upright: glyph_data.is_upright,@@ -474,9 +468,7 @@ impl InlineContent {})});fragment.record.line_index = line_index as u32;- fragment- .record- .finish_text_offsets(arena, fragment.has_trailing_whitespace);+ fragment.record.finish_text_offsets(arena);fragments.push(fragment.record);}}@@ -728,6 +720,8 @@ impl InlineContent {font: run.font.clone(),is_upright: true,bounding_box,+ ascent: run.ascent,+ descent: run.descent,cluster_geometry: Default::default(),}));fragment.baseline = CssPixels::nearest_value_for_f32(ascent);@@ -783,20 +777,8 @@ impl InlineContent {}impl FragmentRecord {- fn finish_text_offsets(&mut self, arena: &LayoutNodeArena, has_trailing_whitespace: bool) {+ fn finish_text_offsets(&mut self, arena: &LayoutNodeArena) {let end = self.start + self.length_in_code_units;- self.trailing_whitespace_length_in_code_units = if has_trailing_whitespace {- arena.text_content(self.layout_node).map_or(0, |content| {- content- .text- .iter()- .skip(end)- .take_while(|&&unit| unit == u16::from(b' ') || unit == u16::from(b'\t'))- .count()- })- } else {- 0- };let dom_offset =|offset, boundary| arena.dom_offset_for_rendered_text_offset(self.layout_node, offset, boundary);self.dom_start_offset_in_node = dom_offset(self.start, super::RenderedTextBoundary::Start);@@ -805,13 +787,11 @@ impl FragmentRecord {} else {dom_offset(end, super::RenderedTextBoundary::End)};- self.dom_end_offset_with_trailing_whitespace = if self.trailing_whitespace_length_in_code_units == 0 {+ let trailing_whitespace_length = self.trimmed_trailing_whitespace.length_in_code_units;+ self.dom_end_offset_with_trailing_whitespace = if trailing_whitespace_length == 0 {self.dom_end_offset_in_node} else {- dom_offset(- end + self.trailing_whitespace_length_in_code_units,- super::RenderedTextBoundary::End,- )+ dom_offset(end + trailing_whitespace_length, super::RenderedTextBoundary::End)};}diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 775486be368..026d9735d02 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -2306,7 +2306,7 @@ impl<'context> InlineFormattingContext<'context> {line_builder.break_if_needed(sequence_inline_size)};if broke_line {- line_builder.set_trailing_whitespace_on_previous_line();+ line_builder.set_trailing_whitespace_on_previous_line(&item);let next_line_start = fragmentation::LinePosition {item_index: item_index + 1,offset_in_node: 0,diff --git a/Libraries/LibWeb/Rust/src/layout/line_box.rs b/Libraries/LibWeb/Rust/src/layout/line_box.rsindex 494555618f5..eea4c604031 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box.rs@@ -155,8 +155,7 @@ impl LineBoxData {last.length_in_code_units += length;last.append_glyph_run(&glyphs.unwrap(), content_inline_size, start - last.start);if trailing_whitespace.length_in_code_units == length {- last.trailing_whitespace.length_in_code_units += trailing_whitespace.length_in_code_units;- last.trailing_whitespace.inline_size += trailing_whitespace.inline_size;+ last.trailing_whitespace += trailing_whitespace;} else {last.trailing_whitespace = trailing_whitespace;}@@ -239,7 +238,8 @@ impl LineBoxData {fn calculate_or_trim_trailing_whitespace(&mut self, should_remove: bool) -> CssPixels {let mut whitespace_inline_size = CssPixels::default();- let mut trailing_whitespace_inline_size = CssPixels::default();+ // The whitespace fragments removed so far that follow on from the same node.+ let mut removed_whitespace: Option<(Node, line_box_fragment::TrailingWhitespace)> = None;let mut fragment_index = self.fragments.len();let last_fragment_index = loop {@@ -259,8 +259,18 @@ impl LineBoxData {}whitespace_inline_size += fragment.inline_length;- trailing_whitespace_inline_size += fragment.inline_length;if should_remove {+ let removed = line_box_fragment::TrailingWhitespace {+ length_in_code_units: fragment.length_in_code_units,+ inline_size: fragment.inline_length,+ };+ removed_whitespace = match removed_whitespace {+ Some((node, mut sum)) if node == fragment.layout_node => {+ sum += removed;+ Some((node, sum))+ }+ _ => Some((fragment.layout_node, removed)),+ };self.record.inline_length -= fragment.inline_length;self.fragments.remove(fragment_index);self.clamp_static_position_markers_to_inline_length();@@ -269,32 +279,26 @@ impl LineBoxData {let last_fragment = &self.fragments[last_fragment_index];if last_fragment.text().is_empty() {- if should_remove && trailing_whitespace_inline_size > CssPixels::default() {- self.fragments[last_fragment_index].has_trailing_whitespace = true;- }return whitespace_inline_size;}// Trim the last fragment's trailing whitespace by the advance recorded when it was shaped.let fragment_trailing_whitespace = last_fragment.trailing_whitespace;whitespace_inline_size += fragment_trailing_whitespace.inline_size;- trailing_whitespace_inline_size += fragment_trailing_whitespace.inline_size;if should_remove {let fragment = &mut self.fragments[last_fragment_index];fragment.length_in_code_units -= fragment_trailing_whitespace.length_in_code_units;fragment.inline_length -= fragment_trailing_whitespace.inline_size;fragment.trailing_whitespace = line_box_fragment::TrailingWhitespace::default();+ fragment.record.trimmed_trailing_whitespace += fragment_trailing_whitespace;+ if let Some((node, removed)) = removed_whitespace+ && node == fragment.layout_node+ {+ fragment.record.trimmed_trailing_whitespace += removed;+ }self.record.inline_length -= fragment_trailing_whitespace.inline_size;self.clamp_static_position_markers_to_inline_length();}-- if should_remove- && (trailing_whitespace_inline_size > CssPixels::default()- || !self.fragments[last_fragment_index].has_trailing_whitespace)- {- self.fragments[last_fragment_index].has_trailing_whitespace =- trailing_whitespace_inline_size > CssPixels::default();- }whitespace_inline_size}@@ -319,7 +323,7 @@ impl LineBoxData {}fragment.length_in_code_units -= whitespace.length_in_code_units;fragment.inline_length -= whitespace.inline_size;- fragment.has_trailing_whitespace = true;+ fragment.record.trimmed_trailing_whitespace += whitespace;break;}self.clamp_static_position_markers_to_inline_length();diff --git a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rsindex aec1e3f49db..cb2a0a243b9 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs@@ -61,13 +61,19 @@ pub(crate) struct TrailingWhitespace {pub(crate) inline_size: CssPixels,}+impl std::ops::AddAssign for TrailingWhitespace {+ fn add_assign(&mut self, other: Self) {+ self.length_in_code_units += other.length_in_code_units;+ self.inline_size += other.inline_size;+ }+}+#[derive(Clone, PartialEq)]pub(crate) struct LineBoxFragmentData {pub(crate) record: inline_content::FragmentRecord,pub(crate) glyphs: Option<GlyphData>,pub(crate) insert_position: f32,pub(crate) current_insert_direction: u8,- pub(crate) has_trailing_whitespace: bool,pub(crate) has_text_overflow_ellipsis: bool,pub(crate) has_soft_wrap_opportunity_after: bool,pub(crate) is_fully_truncated: bool,@@ -139,7 +145,7 @@ impl LineBoxFragmentData {dom_start_offset_in_node: 0,dom_end_offset_in_node: 0,dom_end_offset_with_trailing_whitespace: 0,- trailing_whitespace_length_in_code_units: 0,+ trimmed_trailing_whitespace: TrailingWhitespace::default(),glyph_run: None,};let mut fragment = Self::with_record(record, glyphs);@@ -154,7 +160,6 @@ impl LineBoxFragmentData {glyphs,insert_position: 0.0,current_insert_direction: direction::LTR,- has_trailing_whitespace: false,has_text_overflow_ellipsis: false,has_soft_wrap_opportunity_after: false,is_fully_truncated: false,diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex 2f7ce3d5391..23c1e8722ef 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -1515,13 +1515,24 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {self.current_block_offset = self.current_block_offset.max(clearance);}- pub(crate) fn set_trailing_whitespace_on_previous_line(&mut self) {+ /// Gives the whitespace that a line break before it dropped from the start of the current line to the end of the+ /// previous line, where it follows on from the same text.+ pub(crate) fn set_trailing_whitespace_on_previous_line(&mut self, whitespace: &inline_level_iterator::Item) {if self.line_count() < 2 {return;}let previous = self.line_count() - 2;- if let Some(fragment) = self.line_mut(previous).fragments.last_mut() {- fragment.has_trailing_whitespace = true;+ if let Some(fragment) = self.line_mut(previous).fragments.last_mut()+ && fragment.layout_node == whitespace.node+ && fragment.start+ + fragment.length_in_code_units+ + fragment.trimmed_trailing_whitespace.length_in_code_units+ == whitespace.offset_in_node+ {+ fragment.record.trimmed_trailing_whitespace += line_box_fragment::TrailingWhitespace {+ length_in_code_units: whitespace.length_in_node,+ inline_size: whitespace.inline_size,+ };}}@@ -1599,7 +1610,7 @@ impl TextLayoutBounds {}}-fn font_metric_in_whole_pixels(metric: f32) -> CssPixels {+pub(crate) fn font_metric_in_whole_pixels(metric: f32) -> CssPixels {CssPixels::from_integer(metric.round_ties_even() as i64)}diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rsindex ec198e24019..19003f03542 100644--- a/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rs+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rs@@ -92,13 +92,14 @@ impl HitTestList {crate::painting::hit_test::caret::CaretPositionType::After => {// INTEROP: Fully collapsed whitespace at the end of a text run is not a caret stop.// Keep the whitespace boundary at soft wraps for upstream affinity.- let end = fragment.start- + fragment.length_in_code_units- + fragment.trailing_whitespace_length_in_code_units;- if arena- .rendered_text(fragment.layout_node)- .is_some_and(|rendered| end < rendered.text.len())- {+ let end = fragment.start + fragment.length_in_code_units;+ let is_space_or_tab = |unit: &u16| *unit == u16::from(b' ') || *unit == u16::from(b'\t');+ if arena.rendered_text(fragment.layout_node).is_some_and(|rendered| {+ rendered+ .text+ .get(end..)+ .is_some_and(|rest| !rest.iter().all(is_space_or_tab))+ }) {fragment.dom_end_offset_with_trailing_whitespace} else {fragment.dom_end_offset_in_node@@ -256,7 +257,5 @@ fn fragment_caret_range_rect() else {return CssPixelRect::default();};- crate::painting::text_fragment::rect_for_selection_offsets(paintable_rows, fragment, offsets, || {- crate::painting::text_fragment::first_available_font(paintable_rows, fragment)- })+ crate::painting::text_fragment::rect_for_selection_offsets(paintable_rows, fragment, offsets)}diff --git a/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs b/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rsindex b25aad9cc78..d08e4cfd8c2 100644--- a/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs@@ -695,12 +695,11 @@ impl<'a, O: Observer> PaintRecorder<'a, O> {return;};let layout_arena = self.source;- let first_available_font = || text_fragment::first_available_font(layout_arena, &fragment);let block_container = self.block_container_of_fragment(&fragment);let item = HitTestItem {text_fragment_index: Some(fragment_index),rect: text_fragment::absolute_rect(layout_arena, &fragment),- caret_rect: text_fragment::whole_range_rect(layout_arena, &fragment, first_available_font),+ caret_rect: text_fragment::whole_range_rect(layout_arena, &fragment),caret_line_index: Some(fragment.line_index as usize),block_container,containing_block: layout_arenadiff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rsindex 98d9389af74..716e1425e1f 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs@@ -601,9 +601,7 @@ fn highlight_rects<O: Observer>(start: span.start_code_unit,end: span.end_code_unit,};- text_fragment::rects_for_selection_offsets(recorder.source, fragment, offsets, || {- text_fragment::first_available_font(recorder.source, fragment)- })+ text_fragment::rects_for_selection_offsets(recorder.source, fragment, offsets)}fn paint_text_shadow<O: Observer>(@@ -807,9 +805,7 @@ fn paint_text_fragment<O: Observer>(start: span.start_code_unit,end: span.end_code_unit,};- let range_rects = text_fragment::rects_for_selection_offsets(recorder.source, fragment, offsets, || {- text_fragment::first_available_font(recorder.source, fragment)- });+ let range_rects = text_fragment::rects_for_selection_offsets(recorder.source, fragment, offsets);for range_rect in &range_rects {let span_rect = converter.rounded_device_rect(*range_rect);recorder.recorder.record_clipped_to(span_rect, |recorder| {@@ -825,9 +821,7 @@ fn paint_text_fragment<O: Observer>();});}- let range_rect = text_fragment::rect_for_selection_offsets(recorder.source, fragment, offsets, || {- text_fragment::first_available_font(recorder.source, fragment)- });+ let range_rect = text_fragment::rect_for_selection_offsets(recorder.source, fragment, offsets);decoration_box.x = range_rect.x;decoration_box.width = range_rect.width;}diff --git a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs b/Libraries/LibWeb/Rust/src/painting/text_fragment.rsindex 43336b7c985..2b02ed6ee83 100644--- a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs+++ b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs@@ -112,9 +112,7 @@ pub(crate) fn range_rect(start_offset_in_code_units,end_offset_in_code_units,) {- Some(offsets) => rect_for_selection_offsets(layout_arena, fragment, offsets, || {- first_available_font(layout_arena, fragment)- }),+ Some(offsets) => rect_for_selection_offsets(layout_arena, fragment, offsets),None => CssPixelRect::default(),}}@@ -127,7 +125,7 @@ pub(crate) fn compute_selection_offsets(end_offset_in_code_units: usize,) -> Option<SelectionOffsets> {let length_with_trailing_whitespace =- fragment.length_in_code_units + fragment.trailing_whitespace_length_in_code_units;+ fragment.length_in_code_units + fragment.trimmed_trailing_whitespace.length_in_code_units;let dom_start = fragment.dom_start_offset_in_node;let dom_end = fragment.dom_end_offset_with_trailing_whitespace;let rendered_offset_for_dom_offset = |dom_offset, boundary| {@@ -228,7 +226,7 @@ pub(crate) fn selection_offsets_for_dom_range(end_offset_in_code_units: usize,) -> Option<SelectionOffsets> {let length_with_trailing_whitespace =- fragment.length_in_code_units + fragment.trailing_whitespace_length_in_code_units;+ fragment.length_in_code_units + fragment.trimmed_trailing_whitespace.length_in_code_units;let dom_start = fragment.dom_start_offset_in_node;let dom_end = fragment.dom_end_offset_with_trailing_whitespace;if dom_start > end_offset_in_code_units || dom_end < start_offset_in_code_units {@@ -288,17 +286,11 @@ pub(crate) fn rect_for_selection_offsets(layout_arena: &impl PaintRead,fragment: &FragmentRecord,offsets: SelectionOffsets,- first_available_font: impl FnOnce() -> Option<libgfx_rust::font::FontHandle>,) -> CssPixelRect {let horizontal = is_horizontal(fragment);let mut rect = absolute_rect(layout_arena, fragment);let length = fragment.length_in_code_units;- let font = match &fragment.glyph_run {- Some(run) => Some(run.font.clone()),- None => first_available_font(),- };-let start_in_text = offsets.start.min(length);let end_in_text = offsets.end.min(length);@@ -315,15 +307,17 @@ pub(crate) fn rect_for_selection_offsets(pixel_width = CssPixels::nearest_value_for_f32(right - left);}- if offsets.start > length || offsets.end > length {- let space_width = match &font {- Some(font) => CssPixels::nearest_value_for_f32(font.glyph_width(' ' as u32)),- None => CssPixels::from_raw(0),+ let trimmed = fragment.trimmed_trailing_whitespace;+ if trimmed.length_in_code_units > 0 {+ let trimmed_advance_of = |offset: usize| {+ let units = offset.saturating_sub(length).min(trimmed.length_in_code_units);+ CssPixels::from_raw(+ (i64::from(trimmed.inline_size.raw_value()) * units as i64 / trimmed.length_in_code_units as i64)+ as i32,+ )};- let trailing_units_before_start = offsets.start.saturating_sub(length);- let trailing_units_before_end = offsets.end.saturating_sub(length);- pixel_offset += space_width * trailing_units_before_start;- pixel_width += space_width * (trailing_units_before_end - trailing_units_before_start);+ pixel_offset += trimmed_advance_of(offsets.start);+ pixel_width += trimmed_advance_of(offsets.end) - trimmed_advance_of(offsets.start);}if offsets.start == offsets.end {@@ -338,21 +332,14 @@ pub(crate) fn rect_for_selection_offsets(rect.height = pixel_width;}- if let Some(font) = &font {- let ascent_f = font.facts().ascent;- let descent_f = font.facts().descent;- if ascent_f > 0.0 || descent_f > 0.0 {- // NB: Layout counts the font's ascent and descent in whole pixels, so its text baselines are too.- let ascent = CssPixels::nearest_value_for_f32(ascent_f.round_ties_even());- let descent = CssPixels::nearest_value_for_f32(descent_f.round_ties_even());- let zero = CssPixels::from_raw(0);- let overflow_top = (ascent - fragment.baseline).max(zero);- let overflow_bottom = (descent - secondary_size(rect, horizontal) + fragment.baseline).max(zero);- if horizontal {- rect.inflate(overflow_top, zero, overflow_bottom, zero);- } else {- rect.inflate(zero, overflow_bottom, zero, overflow_top);- }+ if let Some(run) = &fragment.glyph_run {+ let zero = CssPixels::from_raw(0);+ let overflow_top = (run.ascent - fragment.baseline).max(zero);+ let overflow_bottom = (run.descent - secondary_size(rect, horizontal) + fragment.baseline).max(zero);+ if horizontal {+ rect.inflate(overflow_top, zero, overflow_bottom, zero);+ } else {+ rect.inflate(zero, overflow_bottom, zero, overflow_top);}}rect@@ -364,9 +351,8 @@ pub(crate) fn rects_for_selection_offsets(layout_arena: &impl PaintRead,fragment: &FragmentRecord,offsets: SelectionOffsets,- first_available_font: impl FnOnce() -> Option<libgfx_rust::font::FontHandle>,) -> Vec<CssPixelRect> {- let rect = rect_for_selection_offsets(layout_arena, fragment, offsets, first_available_font);+ let rect = rect_for_selection_offsets(layout_arena, fragment, offsets);let length = fragment.length_in_code_units;let extents = fragment.glyph_run.as_ref().map_or_else(Vec::new, |run| {run.cluster_geometry()@@ -395,16 +381,12 @@ pub(crate) fn rects_for_selection_offsets(.collect()}-pub(crate) fn whole_range_rect(- layout_arena: &impl PaintRead,- fragment: &FragmentRecord,- first_available_font: impl FnOnce() -> Option<libgfx_rust::font::FontHandle>,-) -> CssPixelRect {+pub(crate) fn whole_range_rect(layout_arena: &impl PaintRead, fragment: &FragmentRecord) -> CssPixelRect {let offsets = SelectionOffsets {start: 0,end: fragment.length_in_code_units,};- rect_for_selection_offsets(layout_arena, fragment, offsets, first_available_font)+ rect_for_selection_offsets(layout_arena, fragment, offsets)}pub(crate) fn is_block_level_box(layout_arena: &impl PaintRead, fragment: &FragmentRecord) -> bool {@@ -417,16 +399,6 @@ pub(crate) fn style_source(_layout_arena: &impl PaintRead, fragment: &FragmentRefragment.style_source}-pub(crate) fn first_available_font(- layout_arena: &impl PaintRead,- fragment: &FragmentRecord,-) -> Option<libgfx_rust::font::FontHandle> {- let source = style_source(layout_arena, fragment);- layout_arena- .node_style_if_live(source)- .map(|style| style.first_available_font())-}-pub(crate) fn physical_edge_offset(arena: &impl PaintRead,fragment: &FragmentRecord,@@ -460,28 +432,19 @@ pub(crate) fn index_in_node_for_point(} else {(position.y - rect.y).to_float()};- let mut offset = 0;- if let Some(run) = &fragment.glyph_run {- let geometry = run.cluster_geometry();- let length = fragment.length_in_code_units;- let (nearest, distance) = layout_arena+ // NB: A point past the end of the text takes the offset in front of the whitespace the line trimmed.+ let offset = fragment.glyph_run.as_ref().map_or(0, |run| {+ layout_arena.with_grapheme_segmenter(fragment.layout_node, |segmenter| {- geometry.offset_for_x(x, length, |offset| {- segmenter- .next_boundary(fragment.start + offset, false)- .map(|boundary| boundary - fragment.start)- })+ run.cluster_geometry()+ .offset_for_x(x, fragment.length_in_code_units, |offset| {+ segmenter+ .next_boundary(fragment.start + offset, false)+ .map(|boundary| boundary - fragment.start)+ })})- .unwrap_or_default();- offset = nearest;- let space_width = run.font.glyph_width(' ' as u32);- let past_end = geometry.distance_past_end(x, length);- let trailing_spaces =- ((past_end / space_width).round().max(0.0) as usize).min(fragment.trailing_whitespace_length_in_code_units);- if trailing_spaces > 0 && (past_end - space_width * trailing_spaces as f32).abs() <= distance {- offset = length + trailing_spaces;- }- }+ .unwrap_or_default()+ });layout_arena.dom_offset_for_rendered_text_offset(fragment.layout_node,fragment.start + offset,