Fully implement bidi for a line's trailing spaces (UAX#9 L1)

Per UAX#9 L1, any spaces that after line breaking would fall logically
at the end of a line must be reset to the paragraph's initial
embedding level, regardless of the embedding level of those spaces in
the paragraph. Until now, Blink only had partial support for this.

The reason this was hard to implement in Blink is because the
direction of text is computed in a per-paragraph level, splitting the
text into items with a given direction and embedding level. Those
items are then split into item results when line breaking, but item
results must have the same direction as the item.

As part of line breaking, some item results are produced which only
have trailing spaces. The partial support, implemented in
crrev.com/c/2505486, consisted of marking these results with the
`has_only_trailing_spaces` flag, and using this flag to distinguish
them when reordering the item results.

This patch builds on top of that partial fix by adding a final
processing step to line breaking, which splits item results so that
trailing spaces (with a different embedding level as the paragraph)
are split into its own item result, and by marking all trailing spaces
item results as `has_only_trailing_spaces`.

This also fixes bug 1514753, where having a item result with non-base
direction which had logically trailing spaces was causing the text
(rather than the spaces) to hang, because
`LineInfo::ComputeTrailingSpaceWidth` assumed that UAX#9 L1 held.

This patch also modifies some existing WPT tests to make them use a
monospace Arabic webfont (since it's not guaranteed that all OS's have
one), and by changing the widths slightly to work around bug 1409445.

Bug: 316409, 1514753
Change-Id: I1eb751b457d413b5099dad8f95badba9a1b3e5fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5199988
Reviewed-by: Koji Ishii <kojii@chromium.org>
Commit-Queue: Andreu Botella <abotella@igalia.com>
Cr-Commit-Position: refs/heads/main@{#1257309}
diff --git a/css/css-text/white-space/eol-spaces-bidi-001.html b/css/css-text/white-space/eol-spaces-bidi-001.html
index b8bcbe1..570f22d 100644
--- a/css/css-text/white-space/eol-spaces-bidi-001.html
+++ b/css/css-text/white-space/eol-spaces-bidi-001.html
@@ -7,8 +7,13 @@
 <meta name="assert" content="Space before a line break is removed even if reordered to the middle of line by bidi reordering">
 
 <style>
+@font-face {
+    font-family: "Hasubi Mono";
+    src: url("/fonts/hasubi-mono/HasubiMono-Regular.woff2");
+}
+
 div {
-    font-family: monospace;
+    font-family: "Hasubi Mono", monospace;
     border: solid blue;
     font-size: 1.5em;
 }
@@ -16,9 +21,9 @@
     border-color: orange;
     white-space: pre;
 }
-.w5 { width: 5ch; }
-.w6 { width: 6ch; }
-.w7 { width: 7ch; }
+.w5 { width: 5.01ch; } /* .01 to cover floating point errors */
+.w6 { width: 6.01ch; }
+.w7 { width: 7.01ch; }
 
 .blue { background: #aaaaff; }
 .red { background: #ffaaaa; }
diff --git a/css/css-text/white-space/eol-spaces-bidi-002.html b/css/css-text/white-space/eol-spaces-bidi-002.html
index 0efba0a..47a1952 100644
--- a/css/css-text/white-space/eol-spaces-bidi-002.html
+++ b/css/css-text/white-space/eol-spaces-bidi-002.html
@@ -9,8 +9,13 @@
 <meta name="assert" content="Use UAX9L1 to determine which space is at the end of the line, taking into account that such spaces, when pre-wrap, must hang.">
 
 <style>
+@font-face {
+    font-family: "Hasubi Mono";
+    src: url("/fonts/hasubi-mono/HasubiMono-Regular.woff2");
+}
+
 div {
-    font-family: monospace;
+    font-family: "Hasubi Mono", monospace;
     border: solid blue;
     font-size: 1.5em;
     white-space: pre-wrap;
@@ -19,10 +24,10 @@
     border-color: orange;
     white-space: pre;
 }
-.w6 { width: 6ch; }
-.w7 { width: 7ch; }
-.w8 { width: 8ch; }
-.w9 { width: 9ch; }
+.w6 { width: 6.01ch; } /* .01 to cover floating point errors */
+.w7 { width: 7.01ch; }
+.w8 { width: 8.01ch; }
+.w9 { width: 9.01ch; }
 
 .blue { background: #aaaaff; }
 .red { background: #ffaaaa; }
diff --git a/css/css-text/white-space/eol-spaces-bidi-004.html b/css/css-text/white-space/eol-spaces-bidi-004.html
new file mode 100644
index 0000000..6d3ef99
--- /dev/null
+++ b/css/css-text/white-space/eol-spaces-bidi-004.html
@@ -0,0 +1,41 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>CSS Text 3 test: trailing collapsible spaces and bidi</title>
+<link rel="author" title="Andreu Botella" href="mailto:abotella@igalia.com">
+<link rel="help" href="https://drafts.csswg.org/css-text-3/#valdef-white-space-pre-wrap">
+<link rel="help" href="https://drafts.csswg.org/css-text-3/#white-space-phase-2">
+<link rel="match" href="reference/eol-spaces-bidi-004-ref.html">
+<link rel="match" href="reference/eol-spaces-bidi-alt-004-ref.html">
+<meta name="assert" content="Hanging space should take paragraph direction and show at the end of the line (blue box to the left)">
+<link rel="stylesheet" type="text/css" href="/fonts/ahem.css" />
+
+<style>
+  div#test {
+      direction: rtl;
+      text-align: left;
+
+      font: 20px/1 Ahem;
+      margin-left: 20px;
+      background: green;
+      color: red;
+
+      width: 4ch;
+      white-space: pre-wrap;
+  }
+  span {
+      background: blue;
+  }
+  #green-cover {
+    position: absolute;
+    margin-left: 20px;
+    width: 60px;
+    height: 40px;
+    background:
+        linear-gradient(green, green) 0 0 / 60px 20px no-repeat,
+        linear-gradient(green, green) 0 20px / 20px 20px no-repeat;
+    z-index: 1;
+  }
+</style>
+<p>Test passes if a blue box (the white space) is visible at the left start in first line and <strong>no red</strong> is shown.</p>
+<div id="green-cover"></div>
+<div id="test"><span>XXX </span>X</div>
diff --git a/css/css-text/white-space/reference/eol-spaces-bidi-001-ref.html b/css/css-text/white-space/reference/eol-spaces-bidi-001-ref.html
index 430706c..2aa8f4a 100644
--- a/css/css-text/white-space/reference/eol-spaces-bidi-001-ref.html
+++ b/css/css-text/white-space/reference/eol-spaces-bidi-001-ref.html
@@ -4,8 +4,13 @@
 <link rel="author" title="Florian Rivoal" href="https://florian.rivoal.net/">
 
 <style>
+@font-face {
+    font-family: "Hasubi Mono";
+    src: url("/fonts/hasubi-mono/HasubiMono-Regular.woff2");
+}
+
 div {
-    font-family: monospace;
+    font-family: "Hasubi Mono", monospace;
     border: solid blue;
     font-size: 1.5em;
     white-space: pre;
@@ -13,9 +18,9 @@
 .ref {
     border-color: orange;
 }
-.w5 { width: 5ch; }
-.w6 { width: 6ch; }
-.w7 { width: 7ch; }
+.w5 { width: 5.01ch; }
+.w6 { width: 6.01ch; }
+.w7 { width: 7.01ch; }
 
 .blue { background: #aaaaff; }
 .red { background: #ffaaaa; }
diff --git a/css/css-text/white-space/reference/eol-spaces-bidi-002-ref.html b/css/css-text/white-space/reference/eol-spaces-bidi-002-ref.html
index 30a4ffd..05b5546 100644
--- a/css/css-text/white-space/reference/eol-spaces-bidi-002-ref.html
+++ b/css/css-text/white-space/reference/eol-spaces-bidi-002-ref.html
@@ -4,8 +4,13 @@
 <link rel="author" title="Florian Rivoal" href="https://florian.rivoal.net/">
 
 <style>
+@font-face {
+    font-family: "Hasubi Mono";
+    src: url("/fonts/hasubi-mono/HasubiMono-Regular.woff2");
+}
+
 div {
-    font-family: monospace;
+    font-family: "Hasubi Mono", monospace;
     border: solid blue;
     font-size: 1.5em;
     white-space: pre;
@@ -13,10 +18,10 @@
 .ref {
     border-color: orange;
 }
-.w6 { width: 6ch; }
-.w7 { width: 7ch; }
-.w8 { width: 8ch; }
-.w9 { width: 9ch; }
+.w6 { width: 6.01ch; }
+.w7 { width: 7.01ch; }
+.w8 { width: 8.01ch; }
+.w9 { width: 9.01ch; }
 
 .blue { background: #aaaaff; }
 .red { background: #ffaaaa; }
@@ -45,4 +50,3 @@
 
 <div class="w9"><span class=pink> </span>A<span class=blue> </span>B<span class=red> </span>ا<span class=green> </span>ب<span class=yellow> </span></div>
 <div class="w9 ref"><span class=pink> </span>A<span class=blue> </span>B<span class=red> </span>ا<span class=green> </span>ب<span class=yellow> </span></div>
-
diff --git a/css/css-text/white-space/reference/eol-spaces-bidi-004-ref.html b/css/css-text/white-space/reference/eol-spaces-bidi-004-ref.html
new file mode 100644
index 0000000..d845f2a
--- /dev/null
+++ b/css/css-text/white-space/reference/eol-spaces-bidi-004-ref.html
@@ -0,0 +1,21 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>CSS Text 3 test reference</title>
+<link rel="author" title="Andreu Botella" href="mailto:abotella@igalia.com">
+<link rel="stylesheet" type="text/css" href="/fonts/ahem.css" />
+
+<style>
+  div#test {
+      text-align: right;
+
+      font: 20px/1 Ahem;
+      background: linear-gradient(green, green) 1ch 0 / cover no-repeat;
+
+      width: 5ch;
+      white-space: pre;
+
+      color: blue;
+  }
+</style>
+<p>Test passes if a blue box (the white space) is visible at the left start in first line and <strong>no red</strong> is shown.</p>
+<div id="test">X    <br> </div>
diff --git a/css/css-text/white-space/reference/eol-spaces-bidi-alt-004-ref.html b/css/css-text/white-space/reference/eol-spaces-bidi-alt-004-ref.html
new file mode 100644
index 0000000..4d06865
--- /dev/null
+++ b/css/css-text/white-space/reference/eol-spaces-bidi-alt-004-ref.html
@@ -0,0 +1,18 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>CSS Text 3 test reference</title>
+<link rel="author" title="Andreu Botella" href="mailto:abotella@igalia.com">
+<link rel="stylesheet" type="text/css" href="/fonts/ahem.css" />
+
+<style>
+  div#test {
+      font: 20px/1 Ahem;
+      margin-left: 20px;
+      background: green;
+
+      width: 4ch;
+      white-space: pre;
+  }
+</style>
+<p>Test passes if a blue box (the white space) is visible at the left start in first line and <strong>no red</strong> is shown.</p>
+<div id="test"> <br> </div>
diff --git a/fonts/hasubi-mono/HasubiMono-Regular.woff2 b/fonts/hasubi-mono/HasubiMono-Regular.woff2
new file mode 100644
index 0000000..ee8b598
--- /dev/null
+++ b/fonts/hasubi-mono/HasubiMono-Regular.woff2
Binary files differ
diff --git a/fonts/hasubi-mono/LICENSE.txt b/fonts/hasubi-mono/LICENSE.txt
new file mode 100644
index 0000000..d952d62
--- /dev/null
+++ b/fonts/hasubi-mono/LICENSE.txt
@@ -0,0 +1,92 @@
+This Font Software is licensed under the SIL Open Font License,
+Version 1.1.
+
+This license is copied below, and is also available with a FAQ at:
+http://scripts.sil.org/OFL
+
+-----------------------------------------------------------
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
+-----------------------------------------------------------
+
+PREAMBLE
+The goals of the Open Font License (OFL) are to stimulate worldwide
+development of collaborative font projects, to support the font
+creation efforts of academic and linguistic communities, and to
+provide a free and open framework in which fonts may be shared and
+improved in partnership with others.
+
+The OFL allows the licensed fonts to be used, studied, modified and
+redistributed freely as long as they are not sold by themselves. The
+fonts, including any derivative works, can be bundled, embedded,
+redistributed and/or sold with any software provided that any reserved
+names are not used by derivative works. The fonts and derivatives,
+however, cannot be released under any other type of license. The
+requirement for fonts to remain under this license does not apply to
+any document created using the fonts or their derivatives.
+
+DEFINITIONS
+"Font Software" refers to the set of files released by the Copyright
+Holder(s) under this license and clearly marked as such. This may
+include source files, build scripts and documentation.
+
+"Reserved Font Name" refers to any names specified as such after the
+copyright statement(s).
+
+"Original Version" refers to the collection of Font Software
+components as distributed by the Copyright Holder(s).
+
+"Modified Version" refers to any derivative made by adding to,
+deleting, or substituting -- in part or in whole -- any of the
+components of the Original Version, by changing formats or by porting
+the Font Software to a new environment.
+
+"Author" refers to any designer, engineer, programmer, technical
+writer or other person who contributed to the Font Software.
+
+PERMISSION & CONDITIONS
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of the Font Software, to use, study, copy, merge, embed,
+modify, redistribute, and sell modified and unmodified copies of the
+Font Software, subject to the following conditions:
+
+1) Neither the Font Software nor any of its individual components, in
+Original or Modified Versions, may be sold by itself.
+
+2) Original or Modified Versions of the Font Software may be bundled,
+redistributed and/or sold with any software, provided that each copy
+contains the above copyright notice and this license. These can be
+included either as stand-alone text files, human-readable headers or
+in the appropriate machine-readable metadata fields within text or
+binary files as long as those fields can be easily viewed by the user.
+
+3) No Modified Version of the Font Software may use the Reserved Font
+Name(s) unless explicit written permission is granted by the
+corresponding Copyright Holder. This restriction only applies to the
+primary font name as presented to the users.
+
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
+Software shall not be used to promote, endorse or advertise any
+Modified Version, except to acknowledge the contribution(s) of the
+Copyright Holder(s) and the Author(s) or with their explicit written
+permission.
+
+5) The Font Software, modified or unmodified, in part or in whole,
+must be distributed entirely under this license, and must not be
+distributed under any other license. The requirement for fonts to
+remain under this license does not apply to any document created using
+the Font Software.
+
+TERMINATION
+This license becomes null and void if any of the above conditions are
+not met.
+
+DISCLAIMER
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
+COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
+DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
+OTHER DEALINGS IN THE FONT SOFTWARE.
diff --git a/fonts/hasubi-mono/README.md b/fonts/hasubi-mono/README.md
new file mode 100644
index 0000000..20bd1ea
--- /dev/null
+++ b/fonts/hasubi-mono/README.md
@@ -0,0 +1,7 @@
+This font is a monospace Arabic font, which helps test the interaction of bidi
+with line breaking, among other features.
+
+Some operating systems already ship with a monospace Arabic font by default, but
+not all do, so it is better to use a webfont.
+
+This font was obtained from https://github.com/eliheuer/hasubi-mono.