layout: Add support for white-space-collapse: break-spaces (#32388)

This change adds support for `white-space-collapse: break-spaces` and
adds initial parsing support for `overflow-wrap` and `word-break`. The
later two properties are not fully supported, only in their interaction
with `break-spaces`. This is a preliminary change preparing to implement
them.

In addition, `break_and_shape` is now forked and added to Layout 2020.
This function is going to change a lot soon and forking is preparation
for this. More code that is only used by Layout 2013 is moved from `gfx`
to that crate.

Co-authored-by: Rakhi Sharma <atbrakhi@igalia.com>
This commit is contained in:
Martin Robinson 2024-05-30 07:33:07 +02:00 committed by GitHub
parent c0dedf06d6
commit 60b4b6c9f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
96 changed files with 410 additions and 537 deletions

View file

@ -12,8 +12,7 @@ use app_units::Au;
use gfx::font::{self, FontMetrics, FontRef, RunMetrics, ShapingFlags, ShapingOptions};
use gfx::font_cache_thread::FontIdentifier;
use gfx::text::glyph::ByteIndex;
use gfx::text::text_run::TextRun;
use gfx::text::util::{self, CompressionMode};
use gfx::text::util::is_bidi_control;
use log::{debug, warn};
use range::Range;
use style::computed_values::text_rendering::T as TextRendering;
@ -35,6 +34,7 @@ use crate::fragment::{
};
use crate::inline::{InlineFragmentNodeFlags, InlineFragments};
use crate::linked_list::split_off_head;
use crate::text_run::TextRun;
/// Returns the concatenated text of a list of unscanned text fragments.
fn text(fragments: &LinkedList<Fragment>) -> String {
@ -192,7 +192,9 @@ impl TextRunScanner {
font_group = font_context.font_group(font_style);
compression = match in_fragment.white_space_collapse() {
WhiteSpaceCollapse::Collapse => CompressionMode::CompressWhitespaceNewline,
WhiteSpaceCollapse::Preserve => CompressionMode::CompressNone,
WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces => {
CompressionMode::CompressNone
},
WhiteSpaceCollapse::PreserveBreaks => CompressionMode::CompressWhitespace,
};
text_transform = inherited_text_style.text_transform;
@ -712,7 +714,7 @@ impl RunMapping {
) {
let was_empty = *start_position == end_position;
let old_byte_length = run_info.text.len();
*last_whitespace = util::transform_text(
*last_whitespace = transform_text(
&text[(*start_position)..end_position],
compression,
*last_whitespace,
@ -828,3 +830,181 @@ fn is_compatible(a: Script, b: Script) -> bool {
fn is_specific(script: Script) -> bool {
script != Script::Common && script != Script::Inherited
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CompressionMode {
CompressNone,
CompressWhitespace,
CompressWhitespaceNewline,
}
// ported from Gecko's nsTextFrameUtils::TransformText.
//
// High level TODOs:
//
// * Issue #113: consider incoming text state (arabic, etc)
// and propagate outgoing text state (dual of above)
//
// * Issue #114: record skipped and kept chars for mapping original to new text
//
// * Untracked: various edge cases for bidi, CJK, etc.
pub fn transform_text(
text: &str,
mode: CompressionMode,
incoming_whitespace: bool,
output_text: &mut String,
) -> bool {
let out_whitespace = match mode {
CompressionMode::CompressNone => {
for ch in text.chars() {
if is_discardable_char(ch, mode) {
// TODO: record skipped char
} else {
// TODO: record kept char
if ch == '\t' {
// TODO: set "has tab" flag
}
output_text.push(ch);
}
}
false
},
CompressionMode::CompressWhitespace | CompressionMode::CompressWhitespaceNewline => {
let mut in_whitespace: bool = incoming_whitespace;
for ch in text.chars() {
// TODO: discard newlines between CJK chars
let mut next_in_whitespace: bool = is_in_whitespace(ch, mode);
if !next_in_whitespace {
if is_always_discardable_char(ch) {
// revert whitespace setting, since this char was discarded
next_in_whitespace = in_whitespace;
// TODO: record skipped char
} else {
// TODO: record kept char
output_text.push(ch);
}
} else {
/* next_in_whitespace; possibly add a space char */
if in_whitespace {
// TODO: record skipped char
} else {
// TODO: record kept char
output_text.push(' ');
}
}
// save whitespace context for next char
in_whitespace = next_in_whitespace;
} /* /for str::each_char */
in_whitespace
},
};
return out_whitespace;
fn is_in_whitespace(ch: char, mode: CompressionMode) -> bool {
match (ch, mode) {
(' ', _) => true,
('\t', _) => true,
('\n', CompressionMode::CompressWhitespaceNewline) => true,
(_, _) => false,
}
}
fn is_discardable_char(ch: char, mode: CompressionMode) -> bool {
if is_always_discardable_char(ch) {
return true;
}
match mode {
CompressionMode::CompressWhitespaceNewline => ch == '\n',
_ => false,
}
}
fn is_always_discardable_char(ch: char) -> bool {
// TODO: check for soft hyphens.
is_bidi_control(ch)
}
}
#[test]
fn test_transform_compress_none() {
let test_strs = [
" foo bar",
"foo bar ",
"foo\n bar",
"foo \nbar",
" foo bar \nbaz",
"foo bar baz",
"foobarbaz\n\n",
];
let mode = CompressionMode::CompressNone;
for &test in test_strs.iter() {
let mut trimmed_str = String::new();
transform_text(test, mode, true, &mut trimmed_str);
assert_eq!(trimmed_str, test)
}
}
#[test]
fn test_transform_compress_whitespace() {
let test_strs = [
(" foo bar", "foo bar"),
("foo bar ", "foo bar "),
("foo\n bar", "foo\n bar"),
("foo \nbar", "foo \nbar"),
(" foo bar \nbaz", "foo bar \nbaz"),
("foo bar baz", "foo bar baz"),
("foobarbaz\n\n", "foobarbaz\n\n"),
];
let mode = CompressionMode::CompressWhitespace;
for &(test, oracle) in test_strs.iter() {
let mut trimmed_str = String::new();
transform_text(test, mode, true, &mut trimmed_str);
assert_eq!(&*trimmed_str, oracle)
}
}
#[test]
fn test_transform_compress_whitespace_newline() {
let test_strs = vec![
(" foo bar", "foo bar"),
("foo bar ", "foo bar "),
("foo\n bar", "foo bar"),
("foo \nbar", "foo bar"),
(" foo bar \nbaz", "foo bar baz"),
("foo bar baz", "foo bar baz"),
("foobarbaz\n\n", "foobarbaz "),
];
let mode = CompressionMode::CompressWhitespaceNewline;
for &(test, oracle) in test_strs.iter() {
let mut trimmed_str = String::new();
transform_text(test, mode, true, &mut trimmed_str);
assert_eq!(&*trimmed_str, oracle)
}
}
#[test]
fn test_transform_compress_whitespace_newline_no_incoming() {
let test_strs = [
(" foo bar", " foo bar"),
("\nfoo bar", " foo bar"),
("foo bar ", "foo bar "),
("foo\n bar", "foo bar"),
("foo \nbar", "foo bar"),
(" foo bar \nbaz", " foo bar baz"),
("foo bar baz", "foo bar baz"),
("foobarbaz\n\n", "foobarbaz "),
];
let mode = CompressionMode::CompressWhitespaceNewline;
for &(test, oracle) in test_strs.iter() {
let mut trimmed_str = String::new();
transform_text(test, mode, false, &mut trimmed_str);
assert_eq!(trimmed_str, oracle)
}
}