Truly independent web browser
LibGfx+LibWeb: Hang an ideographic space at the end of a line
sideshowbarker86a316951326
diff --git a/Libraries/LibGfx/Rust/src/text_layout.rs b/Libraries/LibGfx/Rust/src/text_layout.rsindex 57cc168b379..8e1f25049fd 100644--- a/Libraries/LibGfx/Rust/src/text_layout.rs+++ b/Libraries/LibGfx/Rust/src/text_layout.rs@@ -162,7 +162,7 @@ unsafe extern "C" {word_spacing: f32,joining_context: JoiningContext,sink: *mut c_void,- emit: unsafe extern "C" fn(*mut c_void, *const ShapedGlyph, usize, f32, usize, f32, bool),+ emit: unsafe extern "C" fn(*mut c_void, *const ShapedGlyph, usize, f32, usize, f32, f32, bool),);fn ladybird_gfx_glyph_run_bounding_box(@@ -663,6 +663,7 @@ pub struct ShapedText {width: f32,trailing_whitespace_length_in_code_units: usize,trailing_whitespace_advance: f32,+ trailing_ascii_whitespace_advance: f32,}impl ShapedText {@@ -690,6 +691,12 @@ impl ShapedText {pub fn trailing_whitespace_advance(&self) -> f32 {self.trailing_whitespace_advance}++ /// The advance of the ASCII white space at the very end alone, out of the trailing white space.+ #[inline]+ pub fn trailing_ascii_whitespace_advance(&self) -> f32 {+ self.trailing_ascii_whitespace_advance+ }}const SINGLE_ASCII_SHAPE_CACHE_SIZE: usize = 128;@@ -725,6 +732,7 @@ struct CachedShape {width: f32,trailing_whitespace_length_in_code_units: usize,trailing_whitespace_advance: f32,+ trailing_ascii_whitespace_advance: f32,requires_context_for_breaks: bool,}@@ -738,6 +746,7 @@ impl CachedShape {&& self.requires_context_for_breaks == other.requires_context_for_breaks&& self.trailing_whitespace_length_in_code_units == other.trailing_whitespace_length_in_code_units&& self.trailing_whitespace_advance.to_bits() == other.trailing_whitespace_advance.to_bits()+ && self.trailing_ascii_whitespace_advance.to_bits() == other.trailing_ascii_whitespace_advance.to_bits()&& self.glyphs.len() == other.glyphs.len()&& std::iter::zip(self.glyphs.placed(), other.glyphs.placed()).all(|(glyph, other_glyph)| glyph.is_bitwise_equal_to(&other_glyph))@@ -753,12 +762,14 @@ impl SharedShape {width: f32,trailing_whitespace_length_in_code_units: usize,trailing_whitespace_advance: f32,+ trailing_ascii_whitespace_advance: f32,) -> Self {Self(Arc::new(CachedShape {glyphs: glyphs.into_shared(),width,trailing_whitespace_length_in_code_units,trailing_whitespace_advance,+ trailing_ascii_whitespace_advance,requires_context_for_breaks: true,}))}@@ -783,6 +794,12 @@ impl SharedShape {self.0.trailing_whitespace_advance}+ /// The advance of the ASCII white space at the very end alone, out of the trailing white space.+ #[inline]+ pub fn trailing_ascii_whitespace_advance(&self) -> f32 {+ self.0.trailing_ascii_whitespace_advance+ }+/// Whether splitting the text can change its glyphs, so that separately shaped chunks might not reproduce it.pub fn requires_context_for_breaks(&self) -> bool {self.0.requires_context_for_breaks@@ -1009,6 +1026,7 @@ fn shape_text_uncached(font: &FontHandle, text: &[u16], params: ShapeParams) ->width: f32,trailing_whitespace_length_in_code_units: usize,trailing_whitespace_advance: f32,+ trailing_ascii_whitespace_advance: f32,requires_context_for_breaks: bool,) {// SAFETY: `sink` is the Option<CachedShape> passed below, and the@@ -1027,6 +1045,7 @@ fn shape_text_uncached(font: &FontHandle, text: &[u16], params: ShapeParams) ->width,trailing_whitespace_length_in_code_units,trailing_whitespace_advance,+ trailing_ascii_whitespace_advance,requires_context_for_breaks,});}@@ -1061,6 +1080,7 @@ fn shaped_text_with_baseline_start(shape: &CachedShape, baseline_start_x: f32) -width: shape.width,trailing_whitespace_length_in_code_units: shape.trailing_whitespace_length_in_code_units,trailing_whitespace_advance: shape.trailing_whitespace_advance,+ trailing_ascii_whitespace_advance: shape.trailing_ascii_whitespace_advance,}}@@ -1393,6 +1413,7 @@ mod shaping_cache_tests {requires_context_for_breaks: false,trailing_whitespace_length_in_code_units: 0,trailing_whitespace_advance: 0.0,+ trailing_ascii_whitespace_advance: 0.0,}}diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cppindex 3fb3da94e6d..0982009dec2 100644--- a/Libraries/LibGfx/TextLayout.cpp+++ b/Libraries/LibGfx/TextLayout.cpp@@ -535,7 +535,18 @@ static bool cluster_takes_letter_spacing(Utf16View const& string, u32 cluster_streturn cluster_start >= string.length_in_code_units() || !is_cursive_script(string.code_point_at(cluster_start));}-static size_t length_of_trailing_whitespace_run(Utf16View const& string)+// https://drafts.csswg.org/css-text-3/#white-space-phase-2+// 4. If there remains any sequence of white space, other space separators, and/or preserved tabs at the end of a line+// (after bidi reordering [CSS-WRITING-MODES-4]):+// - If white-space is set to normal, nowrap, or pre-line, the UA must hang this sequence (unconditionally).+// AD-HOC: Of the other space separators, only the ideographic space (U+3000) counts, so the rest (the em space, e.g.)+// stay on the line.+static bool is_trailing_space(u16 code_unit)+{+ return is_ascii_space(code_unit) || code_unit == 0x3000;+}++static size_t length_of_trailing_ascii_whitespace_run(Utf16View const& string){size_t length = 0;while (length < string.length_in_code_units() && is_ascii_space(string.code_unit_at(string.length_in_code_units() - length - 1)))@@ -543,9 +554,20 @@ static size_t length_of_trailing_whitespace_run(Utf16View const& string)return length;}+static size_t length_of_trailing_whitespace_run(Utf16View const& string)+{+ size_t length = 0;+ while (length < string.length_in_code_units() && is_trailing_space(string.code_unit_at(string.length_in_code_units() - length - 1)))+ ++length;+ return length;+}+struct TrailingWhitespace {size_t length_in_code_units { 0 };float advance { 0 };+ // The advance of the ASCII white space at the very end alone, which intrinsic sizing takes off, since an+ // ideographic space that hangs at the end of a line still counts toward its intrinsic sizes.+ float ascii_advance { 0 };};struct ShapedGlyphs {@@ -573,6 +595,7 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font co// added, spacing included.TrailingWhitespace trailing_whitespace { .length_in_code_units = length_of_trailing_whitespace_run(string), .advance = 0 };auto first_trailing_whitespace_offset = string.length_in_code_units() - trailing_whitespace.length_in_code_units;+ auto first_trailing_ascii_whitespace_offset = string.length_in_code_units() - length_of_trailing_ascii_whitespace_run(string);auto is_right_to_left = hb_buffer_get_direction(buffer) == HB_DIRECTION_RTL;auto cluster_length_in_code_units = [&](auto index) -> size_t {@@ -662,6 +685,8 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font coif (glyph_info[i].cluster >= first_trailing_whitespace_offset)trailing_whitespace.advance += glyphs.last().glyph_width;+ if (glyph_info[i].cluster >= first_trailing_ascii_whitespace_offset)+ trailing_whitespace.ascii_advance += glyphs.last().glyph_width;}hb_buffer_destroy(buffer);@@ -705,7 +730,7 @@ static_assert(offsetof(Gfx::FFI::ShapedGlyph, glyph_id) == offsetof(Gfx::ShapedGstatic_assert(offsetof(Gfx::FFI::ShapedGlyph, should_paint) == offsetof(Gfx::ShapedGlyph, should_paint));extern "C" {-void ladybird_gfx_shape_text_uncached(void const*, u16 const*, size_t, Gfx::FFI::TextType, float, float, Gfx::FFI::JoiningContext, void*, void (*)(void*, Gfx::FFI::ShapedGlyph const*, size_t, float, size_t, float, bool));+void ladybird_gfx_shape_text_uncached(void const*, u16 const*, size_t, Gfx::FFI::TextType, float, float, Gfx::FFI::JoiningContext, void*, void (*)(void*, Gfx::FFI::ShapedGlyph const*, size_t, float, size_t, float, float, bool));float ladybird_gfx_font_measure_text_width(void const*, u16 const*, size_t);void ladybird_gfx_glyph_run_bounding_box(void const*, Gfx::FFI::ShapedGlyph const*, size_t, float, float*);void ladybird_gfx_glyph_run_ink_bounding_box(void const*, Gfx::FFI::ShapedGlyph const*, size_t, float, float*);@@ -721,7 +746,7 @@ extern "C" void ladybird_gfx_shape_text_uncached(float word_spacing,Gfx::FFI::JoiningContext joining_context,void* sink,- void (*emit)(void*, Gfx::FFI::ShapedGlyph const*, size_t, float, size_t, float, bool))+ void (*emit)(void*, Gfx::FFI::ShapedGlyph const*, size_t, float, size_t, float, float, bool)){VERIFY(font);VERIFY(text_utf16 || length_in_code_units == 0);@@ -743,6 +768,7 @@ extern "C" void ladybird_gfx_shape_text_uncached(shape.width,shape.trailing_whitespace.length_in_code_units,shape.trailing_whitespace.advance,+ shape.trailing_whitespace.ascii_advance,shape.requires_context_for_breaks);}diff --git a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rsindex 4d88e3c6234..c929416807e 100644--- a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs+++ b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs@@ -120,7 +120,7 @@ extern "C" fn ladybird_gfx_shape_text_uncached(_word_spacing: f32,_joining_context: JoiningContext,_sink: *mut c_void,- _emit: unsafe extern "C" fn(*mut c_void, *const ShapedGlyph, usize, f32, usize, f32, bool),+ _emit: unsafe extern "C" fn(*mut c_void, *const ShapedGlyph, usize, f32, usize, f32, f32, bool),) {unreachable!("no unit test shapes text");}diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex bf9ab4f28c7..9dfba3482df 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -3966,7 +3966,8 @@ impl ContentSizingLines {&& style.white_space_collapse() == white_space_collapse::PRESERVE&& style.text_wrap_mode() == text_wrap_mode::WRAP){- item.trailing_whitespace.inline_size+ // NB: An ideographic space at the end hangs, but it still counts toward the intrinsic sizes.+ item.trailing_whitespace.ascii_inline_size} else {CssPixels::default()};diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex 056b0e6c92b..f8ebd76371d 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -186,6 +186,7 @@ fn shape_set_upright(width,shape.trailing_whitespace_length_in_code_units(),shape.trailing_whitespace_advance(),+ shape.trailing_ascii_whitespace_advance(),)}@@ -1984,6 +1985,7 @@ impl<'context> RunItemResolver<'_, 'context> {let shaped_trailing_whitespace = line_box_fragment::TrailingWhitespace {length_in_code_units: shape.trailing_whitespace_length_in_code_units(),inline_size: CssPixels::nearest_value_for_f32(shape.trailing_whitespace_advance()),+ ascii_inline_size: CssPixels::nearest_value_for_f32(shape.trailing_ascii_whitespace_advance()),};let chunk_inline_size = CssPixels::nearest_value_for_f32(width + inline_offset);let length_in_node = prepared_item.length_in_node();@@ -1996,6 +1998,7 @@ impl<'context> RunItemResolver<'_, 'context> {line_box_fragment::TrailingWhitespace {length_in_code_units: length_in_node,inline_size: chunk_inline_size,+ ascii_inline_size: chunk_inline_size,}} else {shaped_trailing_whitespacediff --git a/Libraries/LibWeb/Rust/src/layout/line_box.rs b/Libraries/LibWeb/Rust/src/layout/line_box.rsindex 2625cdb5890..fd98b2a8fa4 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box.rs@@ -254,6 +254,24 @@ impl LineBoxData {) {return whitespace_inline_size;}+ // NB: A fragment that's all trailing white space other than a space, such as an element holding just an+ // ideographic space, is trimmed to nothing rather than removed, so a line with nothing else on it stays.+ let is_all_trailing_whitespace = fragment.length_in_code_units > 0+ && fragment.trailing_whitespace.length_in_code_units == fragment.length_in_code_units;+ if is_all_trailing_whitespace && !fragment.is_justifiable_whitespace() {+ let fragment_trailing_whitespace = fragment.trailing_whitespace;+ whitespace_inline_size += fragment_trailing_whitespace.inline_size;+ if should_remove {+ let fragment = &mut self.fragments[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;+ self.record.inline_length -= fragment_trailing_whitespace.inline_size;+ self.clamp_static_position_markers_to_inline_length();+ }+ continue;+ }if !fragment.is_justifiable_whitespace() {break fragment_index;}@@ -263,6 +281,7 @@ impl LineBoxData {let removed = line_box_fragment::TrailingWhitespace {length_in_code_units: fragment.length_in_code_units,inline_size: fragment.inline_length,+ ascii_inline_size: fragment.inline_length,};removed_whitespace = match removed_whitespace {Some((node, mut sum)) if node == fragment.layout_node => {diff --git a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rsindex 604e6c5ea57..61d2e5f406a 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs@@ -59,12 +59,16 @@ impl ItemGlyphRun<'_> {pub(crate) struct TrailingWhitespace {pub(crate) length_in_code_units: usize,pub(crate) inline_size: CssPixels,+ /// The inline size of the ASCII white space at the very end alone, which is what intrinsic sizing takes off: An+ /// ideographic space that hangs at the end of a line still counts toward the intrinsic sizes.+ pub(crate) ascii_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;+ self.ascii_inline_size += other.ascii_inline_size;}}diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex fcb50893881..a15f46df5b8 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -1741,6 +1741,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {fragment.record.trimmed_trailing_whitespace += line_box_fragment::TrailingWhitespace {length_in_code_units: whitespace.length_in_node,inline_size: whitespace.inline_size,+ ascii_inline_size: whitespace.inline_size,};}}diff --git a/Tests/LibWeb/Text/expected/ideographic-space-hangs-at-end-of-line.txt b/Tests/LibWeb/Text/expected/ideographic-space-hangs-at-end-of-line.txtnew file mode 100644index 00000000000..a5bc8c51fa4--- /dev/null+++ b/Tests/LibWeb/Text/expected/ideographic-space-hangs-at-end-of-line.txt@@ -0,0 +1,17 @@+right: ends at the content edge+right-two: ends at the content edge+right-ascii-space: ends at the content edge+right-split: ends at the content edge+right-split-after-one: ends at the content edge+right-split-around-one: ends at the content edge+nowrap: ends at the content edge+pre-line: ends at the content edge+right-em-space: ends short of it+center: centered+only-ideographic-space: takes a line+only-split-ideographic-spaces: takes a line+max-content counts the ideographic spaces: true+max-content counts them across elements: true+max-content drops a space after them: true+min-content counts the ideographic spaces: true+wrap: 2 linesdiff --git a/Tests/LibWeb/Text/input/ideographic-space-hangs-at-end-of-line.html b/Tests/LibWeb/Text/input/ideographic-space-hangs-at-end-of-line.htmlnew file mode 100644index 00000000000..45f9a514cf6--- /dev/null+++ b/Tests/LibWeb/Text/input/ideographic-space-hangs-at-end-of-line.html@@ -0,0 +1,71 @@+<!DOCTYPE html>+<meta charset="utf-8">+<style>+ div {+ font: 20px sans-serif;+ width: 200px;+ }+ .max-content, .min-content {+ display: inline-block;+ font: 20px sans-serif;+ }+ .max-content {+ width: max-content;+ }+ .min-content {+ width: min-content;+ }+</style>+<!-- U+3000 IDEOGRAPHIC SPACE at the end of a line hangs, as the space does. -->+<div id="right" style="text-align: right">ab </div>+<div id="center" style="text-align: center">ab </div>+<div id="right-two" style="text-align: right">ab  </div>+<div id="right-ascii-space" style="text-align: right">ab </div>+<div id="right-split" style="text-align: right">ab<span> </span></div>+<div id="right-split-after-one" style="text-align: right">ab <span> </span></div>+<div id="right-split-around-one" style="text-align: right">ab<span> </span> </div>+<div id="nowrap" style="text-align: right; white-space: nowrap">ab </div>+<div id="pre-line" style="text-align: right; white-space: pre-line">ab </div>+<!-- The em space (U+2003) stays on the line. -->+<div id="right-em-space" style="text-align: right">ab </div>+<!-- A line that holds nothing but ideographic spaces still takes its height. -->+<div id="only-ideographic-space"> </div>+<div id="only-split-ideographic-spaces"><span> </span><span> </span></div>+<!-- An ideographic space at the end still counts toward the intrinsic sizes; a trailing space doesn't. -->+<span class="max-content" id="max-ab">ab</span>+<span class="max-content" id="max-ideographic">ab  </span>+<span class="max-content" id="max-ideographic-split"><span>ab</span><span> </span><span> </span></span>+<span class="max-content" id="max-ascii">ab  </span>+<span class="min-content" id="min-ab">ab</span>+<span class="min-content" id="min-ideographic">ab  </span>+<!-- A line exactly as wide as "ab" still holds "ab" and the ideographic space after it. -->+<div id="wrap"><span>ab</span> cd</div>+<script src="include.js"></script>+<script>+ test(() => {+ const range = document.createRange();+ const gap = div => {+ const text = div.firstChild;+ range.setStart(text, 0);+ range.setEnd(text, 2);+ return Math.round(div.getBoundingClientRect().right - range.getBoundingClientRect().right);+ };+ for (const id of ["right", "right-two", "right-ascii-space", "right-split", "right-split-after-one", "right-split-around-one", "nowrap", "pre-line", "right-em-space"])+ println(`${id}: ${gap(document.getElementById(id)) === 0 ? "ends at the content edge" : "ends short of it"}`);+ const center = document.getElementById("center");+ const left = (() => { range.setStart(center.firstChild, 0); range.setEnd(center.firstChild, 2); return range.getBoundingClientRect().left - center.getBoundingClientRect().left; })();+ println(`center: ${Math.abs(left - gap(center)) <= 1 ? "centered" : "off center"}`);+ for (const id of ["only-ideographic-space", "only-split-ideographic-spaces"])+ println(`${id}: ${document.getElementById(id).getBoundingClientRect().height > 0 ? "takes a line" : "takes no line"}`);+ const width = id => document.getElementById(id).getBoundingClientRect().width;+ println(`max-content counts the ideographic spaces: ${width("max-ideographic") > width("max-ab")}`);+ println(`max-content counts them across elements: ${Math.abs(width("max-ideographic-split") - width("max-ideographic")) < 1}`);+ println(`max-content drops a space after them: ${Math.abs(width("max-ascii") - (width("max-ideographic") + width("max-ab")) / 2) < 1}`);+ println(`min-content counts the ideographic spaces: ${width("min-ideographic") > width("min-ab")}`);+ const wrap = document.getElementById("wrap");+ wrap.style.width = `${wrap.querySelector("span").getBoundingClientRect().width}px`;+ range.selectNodeContents(wrap);+ const lines = new Set([...range.getClientRects()].map(rect => Math.round(rect.top))).size;+ println(`wrap: ${lines} lines`);+ });+</script>