Truly independent web browser
LibWeb: Break right-to-left text where overflow-wrap allows it
sideshowbarkerae429a7b37e8
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex 3a2ca062605..992f0644e86 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -731,13 +731,21 @@ impl Item {take_first_grapheme_when_none_fits: bool,) -> Option<Item> {let run = self.glyph_run(prepared)?;- if run.text_type == line_box_fragment::GLYPH_TEXT_TYPE_RTL || run.glyphs.len() < 2 {+ if run.glyphs.len() < 2 {return None;}let shaped_length_in_code_units: usize = run.glyphs.iter().map(|glyph| glyph.length_in_code_units()).sum();if shaped_length_in_code_units != self.length_in_node {return None;}+ if run.text_type == line_box_fragment::GLYPH_TEXT_TYPE_RTL {+ return self.split_right_to_left_at_shaped_glyph(+ prepared,+ segmenter,+ max_border_box_inline_size,+ take_first_grapheme_when_none_fits,+ );+ }let leading_edge_inline_size = self.margin_start + self.border_start + self.padding_start;let mut split: Option<(usize, usize)> = None;@@ -775,6 +783,79 @@ impl Item {Some(self.take_prefix_before(self.offset_in_node + split_code_units, prefix, remainder))}+ /// Splits off the longest logical prefix of a right-to-left run that fits `max_border_box_inline_size`, from its+ /// shaped glyphs. Those are in visual order, so the logical prefix is the glyphs at the right end of the run.+ fn split_right_to_left_at_shaped_glyph(+ &mut self,+ prepared: &PreparedInlineContent,+ segmenter: &text_chunker::GraphemeSegmenter,+ max_border_box_inline_size: CssPixels,+ take_first_grapheme_when_none_fits: bool,+ ) -> Option<Item> {+ let run = self.glyph_run(prepared)?;+ let placed: Vec<_> = run.glyphs.placed().collect();+ // NB: The spacer glyphs of leading tabs come first, with ascending clusters, so in a run that has them, the+ // logical prefix isn't the glyphs at the right end. Leave such a run to reshaping.+ if placed+ .windows(2)+ .any(|pair| pair[0].cluster_start < pair[1].cluster_start)+ {+ return None;+ }+ let leading_edge_inline_size = self.margin_start + self.border_start + self.padding_start;+ // NB: The prefix is the glyphs from the split on, and the cluster just left of the split starts where it ends.+ let mut split: Option<(usize, usize)> = None;+ for glyph_index in (1..placed.len()).rev() {+ let (left, right) = (&placed[glyph_index - 1], &placed[glyph_index]);+ if left.cluster_start <= right.cluster_start {+ continue;+ }+ let prefix_length = left.cluster_start as usize;+ let offset = self.offset_in_node + prefix_length;+ if segmenter.next_boundary(offset, true) != Some(offset) {+ continue;+ }+ let prefix_inline_size = CssPixels::nearest_value_for_f32(run.width - right.x);+ if leading_edge_inline_size + prefix_inline_size > max_border_box_inline_size {+ if split.is_none() && take_first_grapheme_when_none_fits {+ split = Some((glyph_index, prefix_length));+ }+ break;+ }+ split = Some((glyph_index, prefix_length));+ }+ let (split_glyph_index, prefix_length) = split?;++ let run = self.take_owned_glyph_run(prepared);+ let split_x = run.glyphs.placed().nth(split_glyph_index)?.x;+ let mut prefix_glyphs = libgfx_rust::text_layout::GlyphBuffer::default();+ let mut remainder_glyphs = libgfx_rust::text_layout::GlyphBuffer::default();+ for (glyph_index, placed) in run.glyphs.placed().enumerate() {+ if glyph_index >= split_glyph_index {+ prefix_glyphs.push_placed(libgfx_rust::text_layout::PlacedGlyph {+ x: placed.x - split_x,+ ..placed+ });+ } else {+ remainder_glyphs.push_placed(libgfx_rust::text_layout::PlacedGlyph {+ cluster_start: placed.cluster_start - prefix_length as u32,+ ..placed+ });+ }+ }+ let prefix = OwnedGlyphRun {+ glyphs: prefix_glyphs,+ width: run.width - split_x,+ is_upright: run.is_upright,+ };+ let remainder = OwnedGlyphRun {+ glyphs: remainder_glyphs,+ width: split_x,+ is_upright: run.is_upright,+ };+ Some(self.take_prefix_before(self.offset_in_node + prefix_length, prefix, remainder))+ }+#[allow(clippy::too_many_arguments)]fn split_by_reshaping(&mut self,@@ -791,9 +872,8 @@ impl Item {}let prepared_item = self.prepared_item(prepared);let text_type = prepared_item.text_type();- if text_type == line_box_fragment::GLYPH_TEXT_TYPE_RTL {- return None;- }+ // NB: A right-to-left run splits at a grapheme boundary from its logical start too, so the part that fits is the+ // run's logical beginning, which the line then draws at its right end.let font = prepared.font_of(prepared_item);let start = self.offset_in_node;let end = start + self.length_in_node;diff --git a/Tests/LibWeb/Layout/expected/unbreakable-run-across-inline-box-boundaries.txt b/Tests/LibWeb/Layout/expected/unbreakable-run-across-inline-box-boundaries.txtindex ea56df67696..49a8b3d56b5 100644--- a/Tests/LibWeb/Layout/expected/unbreakable-run-across-inline-box-boundaries.txt+++ b/Tests/LibWeb/Layout/expected/unbreakable-run-across-inline-box-boundaries.txt@@ -400,10 +400,12 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children"bbbb"TextNode <#text> (not painted)BlockContainer <div> at [8,1208] [0+0+0 100 0+0+684] [0+0+0 20 0+0+10] [BFC] children: inline- frag 0 from TextNode start: 0, length: 2, rect: [100.6875,1208 7.3125x10] baseline: 8+ frag 0 from TextNode start: 0, length: 2, rect: [60.46875,1208 7.3125x10] baseline: 8"שש"- frag 1 from TextNode start: 0, length: 20, rect: [34.875,1218 73.125x10] baseline: 8- "שששששששששששששששששששש"+ frag 1 from TextNode start: 0, length: 11, rect: [67.78125,1208 40.21875x10] baseline: 8+ "ששששששששששש"+ frag 2 from TextNode start: 11, length: 9, rect: [75.09375,1218 32.90625x10] baseline: 8+ "ששששששששש"TextNode <#text> (not painted)BlockContainer <span> at [8,1208] floating [0+0+0 50 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inlineTextNode <#text> (not painted)diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/overflow-wrap/reference/overflow-wrap-shaping-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/overflow-wrap/reference/overflow-wrap-shaping-001-ref.htmlnew file mode 100644index 00000000000..4dc6436af27--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/overflow-wrap/reference/overflow-wrap-shaping-001-ref.html@@ -0,0 +1,26 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>CSS Text level 3 Test ref</title>+<link rel="author" title="Florian Rivoal" href="https://florian.rivoal.net/">+<style>+ @font-face {+ font-family: 'csstest_noto';+ src: url('../../../../fonts/noto/NotoNaskhArabic-regular.woff2') format('woff2');+ }+ div {+ font-family: 'csstest_noto';+ font-size: 4em;+ margin: 1rem;+ }+ section {+ float: left; /* to sizing to the intrinsic min of the ref box*/+ }+ #ref { border: solid orange; }+ #test { border: solid blue; }+</style>++<p>Test passes if the blue and organge boxes are identical. Pay attention to characters at the left of the first line, and on the second line.+<section>+<div dir=rtl lang=ar id=ref>ﻋﺎﺋﻠ<br>ﺔ</div>+<div dir=rtl lang=ar id=test>ﻋﺎﺋﻠ<br>ﺔ</div>+</section>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/word-break-break-all-ref-004.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/word-break-break-all-ref-004.htmlnew file mode 100644index 00000000000..e28e571ae4c--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/word-break-break-all-ref-004.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<html lang="en" >+<head>+<meta charset="utf-8">+<title>word-break: break-all, arabic</title>+<link rel='author' title='Richard Ishida' href='mailto:[email protected]'>+<style type='text/css'>+.test { word-break: break-all; }+/* the CSS below is not part of the test */+.test, .ref { border: 1px solid orange; margin: 20px; padding: 10px; width: 390px; font: 36px/1 sans-serif; }+</style>+</head>+<body>+<div id='instructions'>Test passes if the two orange boxes are the same.</div>+<div class="ref" dir="rtl" lang="ar"><span>التدويل نشاط التدوي‍<br/>‍ل</span></div>+<div class="ref" dir="rtl" lang="ar"><span>التدويل نشاط التدوي‍<br/>‍ل</span></div>+</body>+</html>\ No newline at end of filediff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/word-break-normal-ar-ref-000.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/word-break-normal-ar-ref-000.htmlnew file mode 100644index 00000000000..f7479d69176--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/word-break-normal-ar-ref-000.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<html lang="en" >+<head>+<meta charset="utf-8">+<title>word-break: normal, arabic</title>+<link rel='author' title='Richard Ishida' href='mailto:[email protected]'>+<style type='text/css'>+.test { word-break: normal; }+/* the CSS below is not part of the test */+.test, .ref { border: 1px solid orange; margin: 20px; padding: 10px; width: 390px; font: 36px/1 sans-serif; }+</style>+</head>+<body>+<div id='instructions'>Test passes if the two orange boxes are the same.</div>+<div class="ref" lang="ar" dir="rtl"><span>العَرَبِيةُ العَرَبِيةُ<br/>العَرَبِيةُ</span></div>+<div class="ref" lang="ar" dir="rtl"><span>العَرَبِيةُ العَرَبِيةُ<br/>العَرَبِيةُ</span></div>+</body>+</html>\ No newline at end of filediff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/overflow-wrap/overflow-wrap-shaping-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/overflow-wrap/overflow-wrap-shaping-001.htmlnew file mode 100644index 00000000000..0424d295fd0--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/overflow-wrap/overflow-wrap-shaping-001.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>CSS Text level 3 Test: overflow-wrap:break-word and text-shaping</title>+<link rel="author" title="Florian Rivoal" href="https://florian.rivoal.net/">+<link rel="help" href="https://www.w3.org/TR/css-text-3/#word-break-shaping">+<link rel="help" href="https://www.w3.org/TR/css-text-3/#overflow-wrap-property">+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/overflow-wrap/reference/overflow-wrap-shaping-001-ref.html">+<meta name="assert" content="Shaping characters are still shaped as if the word were not broken when a line is broken by overflow-wrap:break-word">+<style>+ @font-face {+ font-family: 'csstest_noto';+ src: url('../../../fonts/noto/NotoNaskhArabic-regular.woff2') format('woff2');+ }+ div {+ font-family: 'csstest_noto';+ font-size: 4em;+ }+ section {+ float: left; /* to sizing to the intrinsic min of the ref box*/+ position: relative;+ }+ #ref {+ border: solid orange;+ margin: 1rem;+ }+ #test {+ border: solid blue;+ position: absolute; /* to avoid influencing the size of the section */+ left: 1rem;+ right: 1rem;+ overflow-wrap: break-word;+ }+</style>++<p>Test passes if the blue and organge boxes are identical. Pay attention to characters at the left of the first line, and on the second line.+<section>+<div dir=rtl lang=ar id=ref>ﻋﺎﺋﻠ<br>ﺔ</div>+<div dir=rtl lang=ar id=test>عائلة</div>+</section>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/overflow-wrap/overflow-wrap-shaping-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/overflow-wrap/overflow-wrap-shaping-002.htmlnew file mode 100644index 00000000000..6f6862d0c62--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/overflow-wrap/overflow-wrap-shaping-002.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>CSS Text level 3 Test: overflow-wrap:anywhere and text-shaping</title>+<link rel="author" title="Florian Rivoal" href="https://florian.rivoal.net/">+<link rel="help" href="https://www.w3.org/TR/css-text-3/#word-break-shaping">+<link rel="help" href="https://www.w3.org/TR/css-text-3/#overflow-wrap-property">+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/overflow-wrap/reference/overflow-wrap-shaping-001-ref.html">+<meta name="assert" content="Shaping characters are still shaped as if the word were not broken when a line is broken by overflow-wrap:anywhere">+<style>+ @font-face {+ font-family: 'csstest_noto';+ src: url('../../../fonts/noto/NotoNaskhArabic-regular.woff2') format('woff2');+ }+ div {+ font-family: 'csstest_noto';+ font-size: 4em;+ }+ section {+ float: left; /* to sizing to the intrinsic min of the ref box*/+ position: relative;+ }+ #ref {+ border: solid orange;+ margin: 1rem;+ }+ #test {+ border: solid blue;+ position: absolute; /* to avoid influencing the size of the section */+ left: 1rem;+ right: 1rem;+ overflow-wrap: anywhere;+ }+</style>++<p>Test passes if the blue and organge boxes are identical. Pay attention to characters at the left of the first line, and on the second line.+<section>+<div dir=rtl lang=ar id=ref>ﻋﺎﺋﻠ<br>ﺔ</div>+<div dir=rtl lang=ar id=test>عائلة</div>+</section>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-004.htmlnew file mode 100644index 00000000000..a565c46868c--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-004.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<html lang="en" >+<head>+<meta charset="utf-8">+<title>word-break: break-all, arabic</title>+<meta name="assert" content="word-break: break-all means lines may break between any two typographic letter units. When shaping scripts such as Arabic are allowed to break within words due to break-all, the characters must still be shaped as if the word were not broken.">+<link rel='help' href='https://drafts.csswg.org/css-text-3/#word-break-property'>+<link rel='match' href='../../../../../expected/wpt-import/css/css-text/word-break/reference/word-break-break-all-ref-004.html'>+<link rel='author' title='Richard Ishida' href='mailto:[email protected]'>+<style type='text/css'>+.test { word-break: break-all; }+/* the CSS below is not part of the test */+.test, .ref { border: 1px solid orange; margin: 20px; padding: 10px; width: 390px; font: 36px/1 sans-serif; }+</style>+</head>+<body>+<div id='instructions'>Test passes if the two orange boxes are the same.</div>+<div class="test" dir="rtl" lang="ar"><div id="testdiv"><span id="testspan">التدويل نشاط التدويل</span></div></div>+<div class="ref" dir="rtl" lang="ar"><span>التدويل نشاط التدوي‍<br/>‍ل</span></div>+<script>+var sentenceWidth = document.getElementById('testspan').offsetWidth+document.getElementById('testdiv').style.width = String(sentenceWidth - 5)+'px'+</script>+</body>+</html>\ No newline at end of filediff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-normal-ar-000.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-normal-ar-000.htmlnew file mode 100644index 00000000000..72de1110d65--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-normal-ar-000.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<html lang="en" >+<head>+<meta charset="utf-8">+<title>word-break: normal, arabic</title>+<meta name="assert" content="word-break: normal means words break according to their customary rules.">+<link rel='help' href='https://drafts.csswg.org/css-text-3/#word-break-property'>+<link rel='match' href='../../../../../expected/wpt-import/css/css-text/word-break/reference/word-break-normal-ar-ref-000.html'>+<link rel='author' title='Richard Ishida' href='mailto:[email protected]'>+<style type='text/css'>+.test { word-break: normal; }+/* the CSS below is not part of the test */+.test, .ref { border: 1px solid orange; margin: 20px; padding: 10px; width: 390px; font: 36px/1 sans-serif; }+</style>+</head>+<body>+<div id='instructions'>Test passes if the two orange boxes are the same.</div>+<div class="test" lang="ar" dir="rtl"><div id="testdiv"><span id="testspan">العَرَبِيةُ العَرَبِيةُ العَرَبِيةُ</span></div></div>+<div class="ref" lang="ar" dir="rtl"><span>العَرَبِيةُ العَرَبِيةُ<br/>العَرَبِيةُ</span></div>+<script>+var sentenceWidth = document.getElementById('testspan').offsetWidth+document.getElementById('testdiv').style.width = String(sentenceWidth - 5)+'px'+</script>+</body>+</html>\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/overflow-break-in-right-to-left-text.txt b/Tests/LibWeb/Text/expected/overflow-break-in-right-to-left-text.txtnew file mode 100644index 00000000000..eea7dafef1b--- /dev/null+++ b/Tests/LibWeb/Text/expected/overflow-break-in-right-to-left-text.txt@@ -0,0 +1,5 @@+hebrew-break-word: breaks true, fits true, lines in logical order true, each line starts at its right end true+hebrew-anywhere: breaks true, fits true, lines in logical order true, each line starts at its right end true+hebrew-word-break-break-word: breaks true, fits true, lines in logical order true, each line starts at its right end true+hebrew-anywhere-in-rtl-block: breaks true, fits true, lines in logical order true, each line starts at its right end true+arabic-anywhere-in-rtl-block: breaks true, fits true, lines in logical order true, each line starts at its right end truediff --git a/Tests/LibWeb/Text/input/overflow-break-in-right-to-left-text.html b/Tests/LibWeb/Text/input/overflow-break-in-right-to-left-text.htmlnew file mode 100644index 00000000000..465a76ce234--- /dev/null+++ b/Tests/LibWeb/Text/input/overflow-break-in-right-to-left-text.html@@ -0,0 +1,40 @@+<!DOCTYPE html>+<meta charset="utf-8">+<style>+ .c { width: 100px; font: 16px/20px sans-serif; }+</style>+<div class="c" id="hebrew-break-word" style="overflow-wrap: break-word">אבגדהוזחטיכלמנסעפצקרשתאבגדהוזחטיכלמנסעפצקרשת</div>+<div class="c" id="hebrew-anywhere" style="overflow-wrap: anywhere">אבגדהוזחטיכלמנסעפצקרשתאבגדהוזחטיכלמנסעפצקרשת</div>+<div class="c" id="hebrew-word-break-break-word" style="word-break: break-word">אבגדהוזחטיכלמנסעפצקרשתאבגדהוזחטיכלמנסעפצקרשת</div>+<div class="c" id="hebrew-anywhere-in-rtl-block" dir="rtl" style="overflow-wrap: anywhere">אבגדהוזחטיכלמנסעפצקרשתאבגדהוזחטיכלמנסעפצקרשת</div>+<div class="c" id="arabic-anywhere-in-rtl-block" dir="rtl" style="overflow-wrap: anywhere">الاستقلاليةالاستقلاليةالاستقلالية</div>+<script src="include.js"></script>+<script>+ test(() => {+ for (const c of document.querySelectorAll(".c")) {+ const text = c.firstChild;+ const box = c.getBoundingClientRect();+ const rectOf = index => {+ const range = document.createRange();+ range.setStart(text, index);+ range.setEnd(text, index + 1);+ return range.getClientRects()[0];+ };+ const lines = [];+ for (let index = 0; index < text.data.length; index++) {+ const rect = rectOf(index);+ let line = lines.find(line => Math.abs(line.top - rect.top) < 1);+ if (!line) {+ line = { top: rect.top, first: index, right: rect.right, inOrder: true, previous: index - 1 };+ lines.push(line);+ }+ line.inOrder &&= line.previous === index - 1;+ line.previous = index;+ line.right = Math.max(line.right, rect.right);+ }+ const firstCharactersAtRightEnd = lines.every(line => rectOf(line.first).right >= line.right - 0.5);+ const inLogicalOrder = lines.every(line => line.inOrder) && lines.every((line, index) => index === 0 || line.top > lines[index - 1].top);+ println(`${c.id}: breaks ${lines.length > 1}, fits ${c.scrollWidth <= box.width}, lines in logical order ${inLogicalOrder}, each line starts at its right end ${firstCharactersAtRightEnd}`);+ }+ });+</script>