Skip to content

Truly independent web browser

LibUnicode+LibWeb: Use ICU's loose line-breaking tailoring

Andreas Kling330af3785be4
diff --git a/Libraries/LibUnicode/Segmenter.cpp b/Libraries/LibUnicode/Segmenter.cpp
index cefbe2fbd3c..3d9ea021979 100644
--- a/Libraries/LibUnicode/Segmenter.cpp
+++ b/Libraries/LibUnicode/Segmenter.cpp
@@ -754,24 +754,28 @@ struct UnicodeLayoutSegmenterHandle {
Vector<char> ascii_storage;
};
+enum class UnicodeLayoutLineBreakMode : u8 {
+ Strict,
+ Normal,
+ Loose,
+};
+
extern "C" {
void* unicode_layout_grapheme_segmenter_create(u16 const*, size_t);
-void* unicode_layout_line_segmenter_create(u16 const*, size_t, bool);
+void* unicode_layout_line_segmenter_create(u16 const*, size_t, UnicodeLayoutLineBreakMode);
void unicode_layout_word_boundaries(u16 const*, size_t, size_t, size_t*, size_t*);
i64 unicode_layout_segmenter_next_boundary(void*, size_t, bool);
void unicode_layout_segmenter_destroy(void*);
}
-static Unicode::Segmenter& unicode_layout_segmenter_prototype(Unicode::SegmenterGranularity granularity, bool allow_conditional_japanese_starters = false)
+static Unicode::Segmenter& unicode_layout_segmenter_prototype(Unicode::SegmenterGranularity granularity, UnicodeLayoutLineBreakMode line_break_mode = UnicodeLayoutLineBreakMode::Strict)
{
static thread_local Unicode::Segmenter* grapheme_prototype = nullptr;
- static thread_local Unicode::Segmenter* line_prototype = nullptr;
- static thread_local Unicode::Segmenter* normal_line_prototype = nullptr;
+ static thread_local Unicode::Segmenter* line_prototypes[3] {};
static thread_local Unicode::Segmenter* word_prototype = nullptr;
auto& prototype = granularity == Unicode::SegmenterGranularity::Grapheme ? grapheme_prototype
: granularity == Unicode::SegmenterGranularity::Word ? word_prototype
- : allow_conditional_japanese_starters ? normal_line_prototype
- : line_prototype;
+ : line_prototypes[to_underlying(line_break_mode)];
if (!prototype) {
// https://drafts.csswg.org/css-text-3/#line-break-property
// The following breaks are forbidden for normal and strict line breaking and allowed in loose:
@@ -779,9 +783,9 @@ static Unicode::Segmenter& unicode_layout_segmenter_prototype(Unicode::Segmenter
// Unicode line breaking class CJ.
// INTEROP: Chrome and Firefox allow breaks before CJ characters for normal as well as loose, despite the
// current specification forbidding them for normal. ICU's normal tailoring resolves CJ to ID.
- // FIXME: Use ICU's loose tailoring for loose, which also allows the other loose-only breaks, such as before
- // iteration marks and between inseparable characters.
- prototype = allow_conditional_japanese_starters
+ prototype = line_break_mode == UnicodeLayoutLineBreakMode::Loose
+ ? Unicode::Segmenter::create("en-u-lb-loose"_utf16, granularity).leak_ptr()
+ : line_break_mode == UnicodeLayoutLineBreakMode::Normal
? Unicode::Segmenter::create("en-u-lb-normal"_utf16, granularity).leak_ptr()
: Unicode::Segmenter::create(granularity).leak_ptr();
}
@@ -802,7 +806,7 @@ extern "C" void* unicode_layout_grapheme_segmenter_create(u16 const* text, size_
return new UnicodeLayoutSegmenterHandle { .segmenter = move(segmenter), .ascii_storage = {} };
}
-extern "C" void* unicode_layout_line_segmenter_create(u16 const* text, size_t length_in_code_units, bool allow_conditional_japanese_starters)
+extern "C" void* unicode_layout_line_segmenter_create(u16 const* text, size_t length_in_code_units, UnicodeLayoutLineBreakMode line_break_mode)
{
auto view = Utf16View { reinterpret_cast<char16_t const*>(text), length_in_code_units };
if (view.is_ascii()) {
@@ -820,7 +824,7 @@ extern "C" void* unicode_layout_line_segmenter_create(u16 const* text, size_t le
};
}
}
- auto segmenter = unicode_layout_segmenter_prototype(Unicode::SegmenterGranularity::Line, allow_conditional_japanese_starters).clone();
+ auto segmenter = unicode_layout_segmenter_prototype(Unicode::SegmenterGranularity::Line, line_break_mode).clone();
segmenter->set_segmented_text(view);
return new UnicodeLayoutSegmenterHandle { .segmenter = move(segmenter), .ascii_storage = {} };
}
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index cb77cd8bbf7..f3157c177ab 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -8,12 +8,19 @@ use super::*;
use libgfx_rust::font::{EmojiPresentation, FontHandle, FrozenFontList, emoji_presentation_for_code_point};
+#[repr(u8)]
+enum LineBreakMode {
+ Strict,
+ Normal,
+ Loose,
+}
+
unsafe extern "C" {
fn unicode_layout_grapheme_segmenter_create(text: *const u16, length_in_code_units: usize) -> *mut c_void;
fn unicode_layout_line_segmenter_create(
text: *const u16,
length_in_code_units: usize,
- allow_conditional_japanese_starters: bool,
+ mode: LineBreakMode,
) -> *mut c_void;
fn unicode_layout_segmenter_next_boundary(handle: *mut c_void, index: usize, inclusive: bool) -> i64;
fn unicode_layout_segmenter_destroy(handle: *mut c_void);
@@ -106,7 +113,7 @@ mod unicode_test_stubs {
extern "C" fn unicode_layout_line_segmenter_create(
_text: *const u16,
_length_in_code_units: usize,
- _allow_conditional_japanese_starters: bool,
+ _mode: super::LineBreakMode,
) -> *mut c_void {
unreachable!("no unit test segments text");
}
@@ -338,12 +345,15 @@ struct LineSegmenter<'text> {
}
impl<'text> LineSegmenter<'text> {
- fn new(text: &'text [u16], allow_conditional_japanese_starters: bool) -> Self {
+ fn new(text: &'text [u16], line_break: u8) -> Self {
+ let mode = match line_break {
+ line_break::LOOSE => LineBreakMode::Loose,
+ line_break::NORMAL => LineBreakMode::Normal,
+ _ => LineBreakMode::Strict,
+ };
// SAFETY: See GraphemeSegmenter::new; the same lifetime contract holds,
// and the C++ side picks the ASCII line-breaking fast path internally.
- let raw = unsafe {
- unicode_layout_line_segmenter_create(text.as_ptr(), text.len(), allow_conditional_japanese_starters)
- };
+ let raw = unsafe { unicode_layout_line_segmenter_create(text.as_ptr(), text.len(), mode) };
Self {
handle: IcuSegmenterHandle { raw },
text,
@@ -527,7 +537,7 @@ impl<'text> TextChunker<'text> {
text: inputs.text,
frozen_font_list: inputs.frozen_font_list,
grapheme_segmenter: GraphemeSegmenter::new(inputs.text),
- line_segmenter: LineSegmenter::new(inputs.text, allow_conditional_japanese_starters),
+ line_segmenter: LineSegmenter::new(inputs.text, inputs.line_break),
word_break: inputs.word_break,
allow_conditional_japanese_starters,
font_variant_emoji: inputs.font_variant_emoji,
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/ja/css-text-line-break-ja-iteration-loose.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/ja/css-text-line-break-ja-iteration-loose.txt
index 98d4893bc97..9967b2ae78f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/ja/css-text-line-break-ja-iteration-loose.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/ja/css-text-line-break-ja-iteration-loose.txt
@@ -2,10 +2,10 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if ja and loose
-Fail 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if ja and loose
-Fail 309D HIRAGANA ITERATION MARK may appear at line start if ja and loose
-Fail 309E HIRAGANA VOICED ITERATION MARK may appear at line start if ja and loose
-Fail 30FD KATAKANA ITERATION MARK may appear at line start if ja and loose
-Fail 30FE KATAKANA VOICED ITERATION MARK may appear at line start if ja and loose
\ No newline at end of file
+6 Pass
+Pass 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if ja and loose
+Pass 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if ja and loose
+Pass 309D HIRAGANA ITERATION MARK may appear at line start if ja and loose
+Pass 309E HIRAGANA VOICED ITERATION MARK may appear at line start if ja and loose
+Pass 30FD KATAKANA ITERATION MARK may appear at line start if ja and loose
+Pass 30FE KATAKANA VOICED ITERATION MARK may appear at line start if ja and loose
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/other-lang/css-text-line-break-de-iteration-loose.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/other-lang/css-text-line-break-de-iteration-loose.txt
index 4d4d86bac10..530a975eb02 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/other-lang/css-text-line-break-de-iteration-loose.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/other-lang/css-text-line-break-de-iteration-loose.txt
@@ -2,10 +2,10 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if de and loose
-Fail 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if de and loose
-Fail 309D HIRAGANA ITERATION MARK may appear at line start if de and loose
-Fail 309E HIRAGANA VOICED ITERATION MARK may appear at line start if de and loose
-Fail 30FD KATAKANA ITERATION MARK may appear at line start if de and loose
-Fail 30FE KATAKANA VOICED ITERATION MARK may appear at line start if de and loose
\ No newline at end of file
+6 Pass
+Pass 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if de and loose
+Pass 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if de and loose
+Pass 309D HIRAGANA ITERATION MARK may appear at line start if de and loose
+Pass 309E HIRAGANA VOICED ITERATION MARK may appear at line start if de and loose
+Pass 30FD KATAKANA ITERATION MARK may appear at line start if de and loose
+Pass 30FE KATAKANA VOICED ITERATION MARK may appear at line start if de and loose
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/unknown-lang/css-text-line-break-iteration-loose.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/unknown-lang/css-text-line-break-iteration-loose.txt
index 9fee00f0337..b3695915f4c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/unknown-lang/css-text-line-break-iteration-loose.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/unknown-lang/css-text-line-break-iteration-loose.txt
@@ -2,10 +2,10 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if lang unknown and loose
-Fail 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if lang unknown and loose
-Fail 309D HIRAGANA ITERATION MARK may appear at line start if lang unknown and loose
-Fail 309E HIRAGANA VOICED ITERATION MARK may appear at line start if lang unknown and loose
-Fail 30FD KATAKANA ITERATION MARK may appear at line start if lang unknown and loose
-Fail 30FE KATAKANA VOICED ITERATION MARK may appear at line start if lang unknown and loose
\ No newline at end of file
+6 Pass
+Pass 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if lang unknown and loose
+Pass 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if lang unknown and loose
+Pass 309D HIRAGANA ITERATION MARK may appear at line start if lang unknown and loose
+Pass 309E HIRAGANA VOICED ITERATION MARK may appear at line start if lang unknown and loose
+Pass 30FD KATAKANA ITERATION MARK may appear at line start if lang unknown and loose
+Pass 30FE KATAKANA VOICED ITERATION MARK may appear at line start if lang unknown and loose
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/zh/css-text-line-break-zh-iteration-loose.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/zh/css-text-line-break-zh-iteration-loose.txt
index d684ab66e6c..3f0d1135413 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/zh/css-text-line-break-zh-iteration-loose.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/i18n/zh/css-text-line-break-zh-iteration-loose.txt
@@ -2,10 +2,10 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if zh and loose
-Fail 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if zh and loose
-Fail 309D HIRAGANA ITERATION MARK may appear at line start if zh and loose
-Fail 309E HIRAGANA VOICED ITERATION MARK may appear at line start if zh and loose
-Fail 30FD KATAKANA ITERATION MARK may appear at line start if zh and loose
-Fail 30FE KATAKANA VOICED ITERATION MARK may appear at line start if zh and loose
\ No newline at end of file
+6 Pass
+Pass 3005 IDEOGRAPHIC ITERATION MARK may appear at line start if zh and loose
+Pass 303B VERTICAL IDEOGRAPHIC ITERATION MARK may appear at line start if zh and loose
+Pass 309D HIRAGANA ITERATION MARK may appear at line start if zh and loose
+Pass 309E HIRAGANA VOICED ITERATION MARK may appear at line start if zh and loose
+Pass 30FD KATAKANA ITERATION MARK may appear at line start if zh and loose
+Pass 30FE KATAKANA VOICED ITERATION MARK may appear at line start if zh and loose
\ No newline at end of file