Truly independent web browser
LibWeb: Put the text-overflow ellipsis on the left in RTL lines
Andreas Kling686726af6107
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 3bda689733f..ed235244ade 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -79,6 +79,11 @@ fn apply_text_overflow_to_line(line: &mut line_box::LineBoxData) {return;}+ if line.direction == direction::RTL {+ apply_text_overflow_to_right_to_left_line(line, available_inline_size);+ return;+ }+let mut line_has_visible_content = false;for index in 0..line.fragments.len() {let fragment_start = line.fragments[index].inline_offset;@@ -139,6 +144,95 @@ fn apply_text_overflow_to_line(line: &mut line_box::LineBoxData) {}}+// NB: The fragments of a line go from left to right, and a right-to-left line overflows its line box on the left. So the+// line keeps its size and its fragments keep their places, and the ellipsis only takes the place of the glyphs+// that are left of the line box.+fn apply_text_overflow_to_right_to_left_line(line: &mut line_box::LineBoxData, available_inline_size: CssPixels) {+ // NB: The line keeps its size, so nothing else tells a line that has its ellipsis from one that still overflows.+ if line+ .fragments+ .iter()+ .any(|fragment| fragment.has_text_overflow_ellipsis)+ {+ return;+ }+ // NB: A line that ended at a forced break is placed already, which moved its fragments.+ let first_visible_position = line.aligned_inline_offset + line.inline_length - available_inline_size;+ let mut line_has_visible_content = false;+ for index in (0..line.fragments.len()).rev() {+ let fragment = &line.fragments[index];+ if fragment.inline_offset >= first_visible_position {+ line_has_visible_content = true;+ continue;+ }+ let Some(glyph_data) = &fragment.glyphs else {+ continue;+ };+ let font = glyph_data.font.clone();+ let ellipsis_inline_size = font.glyph_width(ELLIPSIS_CODE_POINT);+ // NB: Glyph positions are in visual order whatever the direction of the text in the fragment, so keep the+ // glyphs that leave room for the ellipsis between them and the left edge of the line box.+ let first_kept_position =+ (first_visible_position - fragment.inline_offset).to_double() as f32 + ellipsis_inline_size;+ // https://drafts.csswg.org/css-overflow-3/#text-overflow+ // implementations must hide characters and atomic inline-level elements at the end edge of the line as+ // necessary to fit the ellipsis, and place the ellipsis immediately adjacent to the end edge of the remaining+ // inline content.+ // NB: A character is a grapheme cluster, so one that has a glyph left of that position goes as a whole.+ let mut elided_clusters: Vec<_> = glyph_data+ .glyphs+ .placed()+ .filter(|placed| placed.x < first_kept_position)+ .map(|placed| placed.cluster_start)+ .collect();+ elided_clusters.sort_unstable();+ elided_clusters.dedup();+ // The first character or atomic inline-level element on a line must be clipped rather than ellipsed.+ if !line_has_visible_content+ && glyph_data+ .glyphs+ .placed()+ .all(|placed| elided_clusters.binary_search(&placed.cluster_start).is_ok())+ && let Some(rightmost) = glyph_data.glyphs.placed().max_by(|a, b| a.x.total_cmp(&b.x))+ && let Ok(position) = elided_clusters.binary_search(&rightmost.cluster_start)+ {+ elided_clusters.remove(position);+ }+ let kept: Vec<_> = glyph_data+ .glyphs+ .placed()+ .filter(|placed| elided_clusters.binary_search(&placed.cluster_start).is_err())+ .collect();+ let leftmost_kept = kept.iter().min_by(|a, b| a.x.total_cmp(&b.x)).copied();+ let kept_start = leftmost_kept.map_or(fragment.inline_length.to_double() as f32, |placed| placed.x);+ let glyph_block_offset = leftmost_kept.map_or(0.0, |placed| placed.y);++ let fragment = &mut line.fragments[index];+ let glyph_data = fragment.glyphs.as_mut().unwrap();+ glyph_data.glyphs.truncate(0);+ for placed in kept {+ glyph_data.glyphs.push_placed(placed);+ }+ glyph_data.glyphs.push_placed(libgfx_rust::text_layout::PlacedGlyph {+ glyph: libgfx_rust::text_layout::DrawGlyph::new(+ ellipsis_inline_size,+ font.glyph_id_for_code_point(ELLIPSIS_CODE_POINT),+ 1,+ true,+ ),+ x: kept_start - ellipsis_inline_size,+ y: glyph_block_offset,+ cluster_start: 0,+ is_rtl: false,+ });+ fragment.has_text_overflow_ellipsis = true;+ // NB: The fragments before this one are left of the line box, where the box that clips its overflow hides+ // them. They stay as they are, so they keep their part in the size of the line and in what the box can+ // scroll to.+ break;+ }+}+// https://drafts.csswg.org/css-overflow-4/#block-ellipsis// The user agent makes room as necessary of the block overflow ellipsis by displacing content from the end of the// line as if wrapping, until the last soft wrap opportunity that would still allow the entire block overflow@@ -1696,6 +1790,7 @@ impl<'context> InlineFormattingContext<'context> {text_overflow_elision: None,block_level_box: None,uses_last_line_alignment: None,+ aligned_inline_offset: CssPixels::default(),};fragment_start = fragment_end;let line_item_start = item_index;diff --git a/Libraries/LibWeb/Rust/src/layout/line_box.rs b/Libraries/LibWeb/Rust/src/layout/line_box.rsindex 1bc33f2d98d..5ff1a000e06 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box.rs@@ -89,6 +89,9 @@ pub(crate) struct LineBoxData {/// Whether placing the line aligned it as the last line of its block, or a line before a forced break, by/// text-align-last. Justification follows the same decision.pub(crate) uses_last_line_alignment: Option<bool>,+ /// How far placing the line moved its fragments along it. A line that isn't placed yet has its fragments where+ /// its content put them, from the start of the line.+ pub(crate) aligned_inline_offset: CssPixels,pub(crate) record: inline_content::LineRecord,}@@ -116,6 +119,7 @@ impl LineBoxData {text_overflow_elision: None,block_level_box: None,uses_last_line_alignment: None,+ aligned_inline_offset: CssPixels::default(),record: inline_content::LineRecord {direction,writing_mode,diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex a7f7990dd79..2df459a0a25 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -1390,6 +1390,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {}};let inline_offset = aligned_inline_offset(self.line(line_index).inline_length);+ self.line_mut(line_index).aligned_inline_offset = inline_offset;let block_offset = CssPixels::default();let LineAlignment {diff --git a/Tests/LibWeb/Ref/expected/text-overflow-rtl-ref.html b/Tests/LibWeb/Ref/expected/text-overflow-rtl-ref.htmlnew file mode 100644index 00000000000..57851c053a7--- /dev/null+++ b/Tests/LibWeb/Ref/expected/text-overflow-rtl-ref.html@@ -0,0 +1,24 @@+<!doctype html>+<meta charset="utf-8">+<style>+ @font-face {+ font-family: "FiraCode";+ src: url("../../Assets/FiraCode-Latin.woff2");+ }+ div {+ font: 20px/1 FiraCode;+ overflow: hidden;+ white-space: nowrap;+ width: 186px;+ text-align: right;+ }+</style>+<div>…t/src/wheel.rs</div>+<div>…t/src/wheel.rs</div>+<div><b>…t/src/wheel.rs</b></div>+<div><b>…t/</b>src/wheel.rs</div>+<div>…t/src/wheel.rs<br>…t/src/wheel.rs</div>+<div>…t/src/wheel.rs<br>…t/src/wheel.rs</div>+<div><span style="margin-right: 12px">…/src/wheel.rs</span></div>+<div>…t/src/wheel.rs</div>+<div style="width: 180px; direction: rtl"><span style="font-size: 40px">Services/</span>Compositor/Ru…t/src/wheel.rs<br><span style="font-size: 40px">Services/</span>Compositor/Ru…t/src/wheel.rs</div>diff --git a/Tests/LibWeb/Ref/input/text-overflow-rtl.html b/Tests/LibWeb/Ref/input/text-overflow-rtl.htmlnew file mode 100644index 00000000000..8b3864c8651--- /dev/null+++ b/Tests/LibWeb/Ref/input/text-overflow-rtl.html@@ -0,0 +1,27 @@+<!doctype html>+<meta charset="utf-8">+<link rel="match" href="../expected/text-overflow-rtl-ref.html" />+<style>+ @font-face {+ font-family: "FiraCode";+ src: url("../../Assets/FiraCode-Latin.woff2");+ }+ div {+ font: 20px/1 FiraCode;+ direction: rtl;+ overflow: hidden;+ white-space: nowrap;+ width: 186px;+ text-overflow: ellipsis;+ }+</style>+<div>Services/Compositor/Rust/src/wheel.rs</div>+<div><span>Services/Compositor/Rust/src/wheel.rs</span></div>+<div><span>Services/</span><b>Compositor/Rust/src/wheel.rs</b></div>+<div><b>Services/Compositor/Rust/</b><span>src/</span><span>wheel.rs</span></div>+<div>Services/Compositor/Rust/src/wheel.rs<br>Services/Compositor/Rust/src/wheel.rs</div>+<div style="white-space: pre">Services/Compositor/Rust/src/wheel.rs+Services/Compositor/Rust/src/wheel.rs</div>+<div><span style="margin-right: 12px">Services/Compositor/Rust/src/wheel.rs</span></div>+<div><span style="display: inline-block; width: 20px; height: 10px; background: green"></span>Services/Compositor/Rust/src/wheel.rs</div>+<div style="width: 180px"><span style="font-size: 40px">Services/</span>Compositor/Rust/src/wheel.rs<br><span style="font-size: 40px">Services/</span>Compositor/Rust/src/wheel.rs</div>