layout: Add initial support for bidirectional text (BiDi) (#33148)

This adds supports for right-to-left text assigning bidi levels to all
line items when necessary. This includes support for the `dir` attribute
as well as corresponding CSS properties like `unicode-bidi`. It only
implements right-to-left rendering for inline layout at the moment and
doesn't include support for `dir=auto`. Because of missing features,
this causes quite a few tests to start failing, as references become
incorrect due to right-to-left rendering being active in some cases,
but not others (before it didn't exist at all).

Analysis of most of the new failures:

```
- /css/css-flexbox/gap-001-rtl.html
  /css/css-flexbox/gap-004-rtl.html
 - Require implementing BiDi in Flexbox, because the start and
   end inline margins are opposite the order of items.

- /css/CSS2/bidi-text/direction-applies-to-*.xht
  /css/CSS2/bidi-text/direction-applies-to-002.xht
  /css/CSS2/bidi-text/direction-applies-to-003.xht
  /css/CSS2/bidi-text/direction-applies-to-004.xht
  - Broken due to a bug in tables, not allocating the
    right amount of width for a column.

- /css/css-lists/inline-list.html
  - This fails because we wrongly insert a soft wrap opportunity between the
    start of an inline box and its first content.

- /css/css-text/bidi/bidi-lines-001.html
  /css/css-text/bidi/bidi-lines-002.html
  /css/CSS2/text/bidi-flag-emoji.html
  - We do not fully support unicode-bidi: plaintext

- /css/css-text/text-align/text-align-end-010.html
  /css/css-text/text-align/text-align-justify-006.html
  /css/css-text/text-align/text-align-start-010.html
  /html/dom/elements/global-attributes/*
  - We do not support dir=auto yet.

- /css/css-text/white-space/tab-bidi-001.html
  - Servo doesn't support tab stops

- /css/CSS2/positioning/abspos-block-level-001.html
  /css/css-text/word-break/word-break-normal-ar-000.html
  - Do not yet support RTL layout in block

- /css/css-text/white-space/pre-wrap-018.html
  - Even in RTL contexts, spaces at the end of the line must hang and
    not be reordered

- /css/css-text/white-space/trailing-space-and-text-alignment-rtl-002.html
  - We are letting spaces hang with white-space: pre, but they shouldn't
    hang.
```

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Rakhi Sharma <atbrakhi@igalia.com>
This commit is contained in:
Martin Robinson 2024-08-21 07:28:54 -07:00 committed by GitHub
parent 65bd5a3b99
commit 56280c6242
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
189 changed files with 547 additions and 762 deletions

View file

@ -3,10 +3,12 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use app_units::Au;
use style::computed_values::direction::T as Direction;
use style::computed_values::mix_blend_mode::T as ComputedMixBlendMode;
use style::computed_values::position::T as ComputedPosition;
use style::computed_values::transform_style::T as ComputedTransformStyle;
use style::logical_geometry::{Direction, WritingMode};
use style::computed_values::unicode_bidi::T as UnicodeBidi;
use style::logical_geometry::{Direction as AxisDirection, WritingMode};
use style::properties::longhands::backface_visibility::computed_value::T as BackfaceVisiblity;
use style::properties::longhands::box_sizing::computed_value::T as BoxSizing;
use style::properties::longhands::column_span::computed_value::T as ColumnSpan;
@ -117,7 +119,7 @@ impl DisplayLayoutInternal {
}
/// Percentages resolved but not `auto` margins
#[derive(Clone)]
#[derive(Clone, Debug)]
pub(crate) struct PaddingBorderMargin {
pub padding: LogicalSides<Au>,
pub border: LogicalSides<Au>,
@ -163,17 +165,17 @@ impl AspectRatio {
/// Given one side length, compute the other one.
pub(crate) fn compute_dependent_size(
&self,
ratio_dependent_axis: Direction,
ratio_dependent_axis: AxisDirection,
ratio_determining_size: Au,
) -> Au {
match ratio_dependent_axis {
// Calculate the inline size from the block size
Direction::Inline => {
AxisDirection::Inline => {
(ratio_determining_size + self.box_sizing_adjustment.block).scale_by(self.i_over_b) -
self.box_sizing_adjustment.inline
},
// Calculate the block size from the inline size
Direction::Block => {
AxisDirection::Block => {
(ratio_determining_size + self.box_sizing_adjustment.inline)
.scale_by(1.0 / self.i_over_b) -
self.box_sizing_adjustment.block
@ -264,6 +266,7 @@ pub(crate) trait ComputedValuesExt {
) -> Option<AspectRatio>;
fn background_is_transparent(&self) -> bool;
fn get_webrender_primitive_flags(&self) -> wr::PrimitiveFlags;
fn bidi_control_chars(&self) -> (&'static str, &'static str);
}
impl ComputedValuesExt for ComputedValues {
@ -752,6 +755,31 @@ impl ComputedValuesExt for ComputedValues {
BackfaceVisiblity::Hidden => wr::PrimitiveFlags::empty(),
}
}
/// If the 'unicode-bidi' property has a value other than 'normal', return the bidi control codes
/// to inject before and after the text content of the element.
/// See the table in <http://dev.w3.org/csswg/css-writing-modes/#unicode-bidi>.
fn bidi_control_chars(&self) -> (&'static str, &'static str) {
match (
self.get_text().unicode_bidi,
self.get_inherited_box().direction,
) {
(UnicodeBidi::Normal, _) => ("", ""),
(UnicodeBidi::Embed, Direction::Ltr) => ("\u{202a}", "\u{202c}"),
(UnicodeBidi::Embed, Direction::Rtl) => ("\u{202b}", "\u{202c}"),
(UnicodeBidi::Isolate, Direction::Ltr) => ("\u{2066}", "\u{2069}"),
(UnicodeBidi::Isolate, Direction::Rtl) => ("\u{2067}", "\u{2069}"),
(UnicodeBidi::BidiOverride, Direction::Ltr) => ("\u{202d}", "\u{202c}"),
(UnicodeBidi::BidiOverride, Direction::Rtl) => ("\u{202e}", "\u{202c}"),
(UnicodeBidi::IsolateOverride, Direction::Ltr) => {
("\u{2068}\u{202d}", "\u{202c}\u{2069}")
},
(UnicodeBidi::IsolateOverride, Direction::Rtl) => {
("\u{2068}\u{202e}", "\u{202c}\u{2069}")
},
(UnicodeBidi::Plaintext, _) => ("\u{2068}", "\u{2069}"),
}
}
}
impl From<stylo::Display> for Display {