Truly independent web browser
LibWeb: Preserve shaping runs when shortening a clamped line
sideshowbarkera81abee527c3
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 87cc65c7aed..72358ea59e0 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -195,8 +195,7 @@ fn apply_block_ellipsis(None,));}- let offsets = fragment.internal_soft_wrap_offsets.as_ref()?;- let glyphs = fragment.glyphs.as_ref()?;+ let offsets = &fragment.block_ellipsis_text.as_ref()?.soft_wrap_offsets;let style = context.style(fragment.style_source);let text_content = context.pass.text_content(fragment.layout_node);offsets.iter().rev().find_map(|offset| {@@ -204,33 +203,15 @@ fn apply_block_ellipsis(return None;}let length = *offset - fragment.start;- let shape = libgfx_rust::text_layout::shape_text_shared(- &glyphs.font,- &fragment.text()[..length],- libgfx_rust::text_layout::TextType::try_from(glyphs.text_type)- .expect("invalid Gfx::GlyphRun::TextType"),+ let (inline_size, glyphs) = fragment.reshape_prefix_for_block_ellipsis(+ &text_content.text,+ *offset,style.letter_spacing().to_double() as f32,style.word_spacing().to_double() as f32,- text_chunker::joining_context(&text_content.text, fragment.start..*offset),);- let inline_size = CssPixels::nearest_value_for_f32(shape.width());let inline_offset = fragment.inline_offset + inline_size;- (inline_offset <= available_for_content).then(|| {- (- index + 1,- inline_offset,- length,- inline_size,- Some(line_box_fragment::GlyphData {- glyphs: shape.glyphs().clone(),- font: glyphs.font.clone(),- text_type: glyphs.text_type,- width: shape.width(),- is_upright: glyphs.is_upright,- derived_placements_end: None,- }),- )- })+ (inline_offset <= available_for_content)+ .then(|| (index + 1, inline_offset, length, inline_size, Some(glyphs)))})})};diff --git a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rsindex 8d033df8464..604e6c5ea57 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs@@ -68,6 +68,13 @@ impl std::ops::AddAssign for TrailingWhitespace {}}+#[derive(Clone, Default, PartialEq)]+pub(crate) struct BlockEllipsisText {+ pub(crate) soft_wrap_offsets: Vec<usize>,+ /// Shaping run ends as UTF-16 offsets in the text node, paired with their original text types.+ pub(crate) runs: Vec<(usize, u8)>,+}+#[derive(Clone, PartialEq)]pub(crate) struct LineBoxFragmentData {pub(crate) record: inline_content::FragmentRecord,@@ -76,9 +83,8 @@ pub(crate) struct LineBoxFragmentData {pub(crate) current_insert_direction: u8,pub(crate) has_text_overflow_ellipsis: bool,pub(crate) has_soft_wrap_opportunity_after: bool,- /// Internal soft wraps for standard block ellipses, as UTF-16 offsets in the text node.- #[allow(clippy::box_collection, reason = "store optional wrap offsets in one word")]- pub(crate) internal_soft_wrap_offsets: Option<Box<Vec<usize>>>,+ /// Shaping boundaries and internal soft wraps needed to shorten text for a standard block ellipsis.+ pub(crate) block_ellipsis_text: Option<Box<BlockEllipsisText>>,pub(crate) is_fully_truncated: bool,pub(crate) trailing_whitespace: TrailingWhitespace,pub(crate) text_utf16: *const u16,@@ -165,7 +171,7 @@ impl LineBoxFragmentData {current_insert_direction: direction::LTR,has_text_overflow_ellipsis: false,has_soft_wrap_opportunity_after: false,- internal_soft_wrap_offsets: None,+ block_ellipsis_text: None,is_fully_truncated: false,trailing_whitespace: TrailingWhitespace::default(),text_utf16: std::ptr::null(),@@ -199,6 +205,60 @@ impl LineBoxFragmentData {unsafe { ak::slice_from_raw_parts(self.text_utf16.add(self.start), self.length_in_code_units) }}+ pub(crate) fn reshape_prefix_for_block_ellipsis(+ &self,+ full_text: &[u16],+ end: usize,+ letter_spacing: f32,+ word_spacing: f32,+ ) -> (CssPixels, GlyphData) {+ let glyphs = self.glyphs.as_ref().expect("a text prefix has glyphs");+ let text = self+ .block_ellipsis_text+ .as_ref()+ .expect("a text prefix has shaping boundaries");+ let mut prefix: Option<Self> = None;+ let mut start = self.start;+ for &(run_end, text_type) in &text.runs {+ let run_end = run_end.min(end);+ if run_end <= start {+ continue;+ }+ let shape = libgfx_rust::text_layout::shape_text_shared(+ &glyphs.font,+ &full_text[start..run_end],+ libgfx_rust::text_layout::TextType::try_from(text_type).expect("invalid Gfx::GlyphRun::TextType"),+ letter_spacing,+ word_spacing,+ text_chunker::joining_context(full_text, start..run_end),+ );+ let inline_size = CssPixels::nearest_value_for_f32(shape.width());+ let run = ItemGlyphRun {+ glyphs: std::borrow::Cow::Borrowed(shape.glyphs()),+ font: &glyphs.font,+ text_type,+ width: shape.width(),+ is_upright: glyphs.is_upright,+ };+ if let Some(prefix) = &mut prefix {+ prefix.append_glyph_run(&run, inline_size, start - self.start);+ } else {+ let mut record = self.record.clone();+ record.inline_length = inline_size;+ prefix = Some(Self::with_record(record, Some(run.into_glyph_data())));+ }+ start = run_end;+ if start == end {+ break;+ }+ }+ let prefix = prefix.expect("a soft wrap retains a nonempty text prefix");+ let inline_size = prefix.inline_length;+ let mut glyphs = prefix.glyphs.unwrap();+ glyphs.width = inline_size.to_double() as f32;+ (inline_size, glyphs)+ }+pub(crate) fn ends_in_whitespace(&self) -> bool {self.text().last().is_some_and(|code_unit| is_ascii_space(*code_unit))}diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex 07d82ec14b4..a681dcf9778 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -637,6 +637,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {style: StyleValues,) {let glyphs = item.take_glyph_run(prepared);+ let text_type = glyphs.text_type;let content_block_size = TextLayoutBounds::new(style, Some(glyphs.font)).block_size();self.append_text_chunk(item.node,@@ -654,17 +655,15 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {// https://drafts.csswg.org/css-overflow-4/#block-ellipsis// AD-HOC: Block ellipses displace content at soft wraps. Legacy clamping preserves glyph-based truncation// inside shaped runs, so only standard clamping keeps these internal opportunities.- if self.context().parent.has_line_clamp()- && !self.context().parent.clamped_content_is_scrollable_overflow()- && item.has_soft_wraps(prepared)- {+ if self.context().parent.has_line_clamp() && !self.context().parent.clamped_content_is_scrollable_overflow() {let line_index = self.line_count() - 1;let mut line = self.line_mut(line_index);let fragment = line.fragments.last_mut().unwrap();- fragment- .internal_soft_wrap_offsets- .get_or_insert_default()- .extend(item.soft_wrap_offsets(prepared));+ let text = fragment.block_ellipsis_text.get_or_insert_default();+ text.runs.push((item.offset_in_node + item.length_in_node, text_type));+ if item.has_soft_wraps(prepared) {+ text.soft_wrap_offsets.extend(item.soft_wrap_offsets(prepared));+ }}}diff --git a/Tests/LibWeb/Ref/expected/line-clamp-internal-soft-wrap-bidi-ref.html b/Tests/LibWeb/Ref/expected/line-clamp-internal-soft-wrap-bidi-ref.htmlnew file mode 100644index 00000000000..5b90ad4acfc--- /dev/null+++ b/Tests/LibWeb/Ref/expected/line-clamp-internal-soft-wrap-bidi-ref.html@@ -0,0 +1,14 @@+<!doctype html>+<html class="reftest-wait">+<meta charset="utf-8">+<style>+ @font-face { font-family: Test; src: url("wpt-import/fonts/mplus-1p-regular.woff"); }+ body { margin: 0; font: 24px/36px Test; }+ .case { line-clamp: 1; word-break: break-all; overflow: hidden; margin-bottom: 4px; background: #eee; }+</style>+<div class="case" style="width: 80px">(אבְג‏<bdi dir="ltr">…</bdi></div>+<div class="case" style="width: 120px">(אבְגדְהוְז‏<bdi dir="ltr">…</bdi></div>+<div class="case" style="width: 160px">(אבְגדְהוְזחְ(א‏<bdi dir="ltr">…</bdi></div>+<script>+ document.fonts.ready.then(() => document.documentElement.classList.remove("reftest-wait"));+</script>diff --git a/Tests/LibWeb/Ref/input/line-clamp-internal-soft-wrap-bidi.html b/Tests/LibWeb/Ref/input/line-clamp-internal-soft-wrap-bidi.htmlnew file mode 100644index 00000000000..1396df65c7c--- /dev/null+++ b/Tests/LibWeb/Ref/input/line-clamp-internal-soft-wrap-bidi.html@@ -0,0 +1,15 @@+<!doctype html>+<html class="reftest-wait">+<meta charset="utf-8">+<link rel="match" href="../expected/line-clamp-internal-soft-wrap-bidi-ref.html">+<style>+ @font-face { font-family: Test; src: url("wpt-import/fonts/mplus-1p-regular.woff"); }+ body { margin: 0; font: 24px/36px Test; }+ .case { line-clamp: 1; word-break: break-all; overflow: hidden; margin-bottom: 4px; background: #eee; }+</style>+<div class="case" style="width: 80px">(אבְגדְהוְזחְ(אבְגדְהוְזחְ(אבְגדְהוְזחְ</div>+<div class="case" style="width: 120px">(אבְגדְהוְזחְ(אבְגדְהוְזחְ(אבְגדְהוְזחְ</div>+<div class="case" style="width: 160px">(אבְגדְהוְזחְ(אבְגדְהוְזחְ(אבְגדְהוְזחְ</div>+<script>+ document.fonts.ready.then(() => document.documentElement.classList.remove("reftest-wait"));+</script>