Truly independent web browser
LibGfx+LibWeb: Keep Arabic letters joined with `word-break: break-all`
Tim Ledbetter246819f86282
diff --git a/Libraries/LibGfx/Rust/src/font.rs b/Libraries/LibGfx/Rust/src/font.rsindex 02eb7b07f29..1604c458605 100644--- a/Libraries/LibGfx/Rust/src/font.rs+++ b/Libraries/LibGfx/Rust/src/font.rs@@ -195,7 +195,14 @@ impl FontHandle {let encoded = char::from_u32(code_point).unwrap_or(char::REPLACEMENT_CHARACTER).encode_utf16(&mut utf16);- crate::text_layout::shaped_text_width(self, encoded, crate::text_layout::TextType::Common, 0.0, 0.0)+ crate::text_layout::shaped_text_width(+ self,+ encoded,+ crate::text_layout::TextType::Common,+ 0.0,+ 0.0,+ crate::text_layout::JoiningContext::default(),+ )}pub fn glyph_id_for_code_point(&self, code_point: u32) -> u32 {diff --git a/Libraries/LibGfx/Rust/src/text_layout.rs b/Libraries/LibGfx/Rust/src/text_layout.rsindex 972b0ffd387..80efc0693cc 100644--- a/Libraries/LibGfx/Rust/src/text_layout.rs+++ b/Libraries/LibGfx/Rust/src/text_layout.rs@@ -34,6 +34,15 @@ impl TryFrom<u8> for TextType {}}+/// Whether the letters at the edges of a text join letters outside it, as the letters of cursive scripts such as+/// Arabic do when a word is shaped in pieces.+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]+#[repr(C)]+pub struct JoiningContext {+ pub joins_before: bool,+ pub joins_after: bool,+}+/// A glyph as shaping makes it and as the glyph run readers in C++ read it.#[derive(Clone, Copy, Debug, Default, PartialEq)]#[repr(C)]@@ -151,6 +160,7 @@ unsafe extern "C" {text_type: TextType,letter_spacing: f32,word_spacing: f32,+ joining_context: JoiningContext,sink: *mut c_void,emit: unsafe extern "C" fn(*mut c_void, *const ShapedGlyph, usize, f32, usize, f32),);@@ -695,6 +705,7 @@ struct ShapeParams {text_type: TextType,letter_spacing: f32,word_spacing: f32,+ joining_context: JoiningContext,}impl ShapeParams {@@ -704,6 +715,7 @@ impl ShapeParams {self.text_type == other.text_type&& self.letter_spacing.to_bits() == other.letter_spacing.to_bits()&& self.word_spacing.to_bits() == other.word_spacing.to_bits()+ && self.joining_context == other.joining_context}}@@ -839,6 +851,7 @@ impl FontShapeCache {&& params.text_type == TextType::Common&& params.letter_spacing == 0.0&& params.word_spacing == 0.0+ && params.joining_context == JoiningContext::default(){return self.single_ascii_common_shapes[code_unit as usize].get_or_insert_with(|| Arc::new(shape_uncached()));@@ -980,13 +993,7 @@ thread_local! {static SHAPING_CACHE: std::cell::RefCell<ShapingCache> = std::cell::RefCell::new(ShapingCache::default());}-fn shape_text_uncached(- font: &FontHandle,- text: &[u16],- text_type: TextType,- letter_spacing: f32,- word_spacing: f32,-) -> CachedShape {+fn shape_text_uncached(font: &FontHandle, text: &[u16], params: ShapeParams) -> CachedShape {unsafe extern "C" fn emit(sink: *mut c_void,glyphs: *const ShapedGlyph,@@ -1023,9 +1030,10 @@ fn shape_text_uncached(font.as_raw(),text.as_ptr(),text.len(),- text_type,- letter_spacing,- word_spacing,+ params.text_type,+ params.letter_spacing,+ params.word_spacing,+ params.joining_context,(&raw mut shape).cast(),emit,);@@ -1049,20 +1057,11 @@ fn shaped_text_with_baseline_start(shape: &CachedShape, baseline_start_x: f32) -fn with_cached_shape<R>(font: &FontHandle,text: &[u16],- text_type: TextType,- letter_spacing: f32,- word_spacing: f32,+ params: ShapeParams,use_shape: impl FnOnce(&Arc<CachedShape>) -> R,) -> R {- let params = ShapeParams {- text_type,- letter_spacing,- word_spacing,- };SHAPING_CACHE.with_borrow_mut(|cache| {- use_shape(&cache.shape_for(font.id().0, text, params, || {- shape_text_uncached(font, text, text_type, letter_spacing, word_spacing)- }))+ use_shape(&cache.shape_for(font.id().0, text, params, || shape_text_uncached(font, text, params)))})}@@ -1072,10 +1071,15 @@ pub fn shape_text_shared(text_type: TextType,letter_spacing: f32,word_spacing: f32,+ joining_context: JoiningContext,) -> SharedShape {- with_cached_shape(font, text, text_type, letter_spacing, word_spacing, |shape| {- SharedShape(shape.clone())- })+ let params = ShapeParams {+ text_type,+ letter_spacing,+ word_spacing,+ joining_context,+ };+ with_cached_shape(font, text, params, |shape| SharedShape(shape.clone()))}pub fn shaped_text_width(@@ -1084,8 +1088,15 @@ pub fn shaped_text_width(text_type: TextType,letter_spacing: f32,word_spacing: f32,+ joining_context: JoiningContext,) -> f32 {- with_cached_shape(font, text, text_type, letter_spacing, word_spacing, |shape| shape.width)+ let params = ShapeParams {+ text_type,+ letter_spacing,+ word_spacing,+ joining_context,+ };+ with_cached_shape(font, text, params, |shape| shape.width)}pub fn shape_text(@@ -1096,7 +1107,13 @@ pub fn shape_text(letter_spacing: f32,word_spacing: f32,) -> ShapedText {- with_cached_shape(font, text, text_type, letter_spacing, word_spacing, |shape| {+ let params = ShapeParams {+ text_type,+ letter_spacing,+ word_spacing,+ joining_context: JoiningContext::default(),+ };+ with_cached_shape(font, text, params, |shape| {shaped_text_with_baseline_start(shape, baseline_start_x)})}@@ -1373,6 +1390,7 @@ mod shaping_cache_tests {text_type: TextType::Common,letter_spacing,word_spacing: 0.0,+ joining_context: JoiningContext::default(),}}diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cppindex f588b7cb909..75445bdb719 100644--- a/Libraries/LibGfx/TextLayout.cpp+++ b/Libraries/LibGfx/TextLayout.cpp@@ -408,10 +408,16 @@ Vector<float> glyph_run_glyph_intercepts(Font const& font, ReadonlySpan<ShapedGlreturn intervals;}-static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, FFI::TextType text_type)+static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, FFI::TextType text_type, FFI::JoiningContext joining_context = {}){hb_buffer_t* buffer = hb_buffer_create();+ // A zero width joiner as context before or after the text gives the letters at its edges the forms that join them+ // to letters outside it.+ static constexpr hb_codepoint_t ZERO_WIDTH_JOINER = 0x200D;+ if (joining_context.joins_before)+ hb_buffer_add_codepoints(buffer, &ZERO_WIDTH_JOINER, 1, 1, 0);+if (string.has_ascii_storage()) {hb_buffer_add_utf8(buffer, string.ascii_span().data(), string.length_in_code_units(), 0, -1);// Fast path for ASCII: we know it's Latin script, LTR direction.@@ -434,6 +440,9 @@ static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font}}+ if (joining_context.joins_after)+ hb_buffer_add_codepoints(buffer, &ZERO_WIDTH_JOINER, 1, 0, 0);+auto* hb_font = font.harfbuzz_font();hb_feature_t const* hb_features_data = nullptr;Vector<hb_feature_t, 4> hb_features;@@ -486,10 +495,10 @@ struct ShapedGlyphs {TrailingWhitespace trailing_whitespace;};-static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font const& font, FFI::TextType text_type, float letter_spacing, float word_spacing)+static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font const& font, FFI::TextType text_type, float letter_spacing, float word_spacing, FFI::JoiningContext joining_context){auto const& metrics = font.pixel_metrics();- auto* buffer = setup_text_shaping(string, font, text_type);+ auto* buffer = setup_text_shaping(string, font, text_type, joining_context);u32 glyph_count;auto const* glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);@@ -602,7 +611,7 @@ static_assert(offsetof(Gfx::FFI::ShapedGlyph, glyph_id) == offsetof(Gfx::ShapedGstatic_assert(offsetof(Gfx::FFI::ShapedGlyph, should_paint) == offsetof(Gfx::ShapedGlyph, should_paint));extern "C" {-void ladybird_gfx_shape_text_uncached(void const*, u16 const*, size_t, Gfx::FFI::TextType, float, float, void*, void (*)(void*, Gfx::FFI::ShapedGlyph const*, size_t, float, size_t, float));+void ladybird_gfx_shape_text_uncached(void const*, u16 const*, size_t, Gfx::FFI::TextType, float, float, Gfx::FFI::JoiningContext, void*, void (*)(void*, Gfx::FFI::ShapedGlyph const*, size_t, float, size_t, float));float ladybird_gfx_font_measure_text_width(void const*, u16 const*, size_t);void ladybird_gfx_glyph_run_bounding_box(void const*, Gfx::FFI::ShapedGlyph const*, size_t, float, float*);void ladybird_gfx_glyph_run_ink_bounding_box(void const*, Gfx::FFI::ShapedGlyph const*, size_t, float, float*);@@ -616,6 +625,7 @@ extern "C" void ladybird_gfx_shape_text_uncached(Gfx::FFI::TextType text_type,float letter_spacing,float word_spacing,+ Gfx::FFI::JoiningContext joining_context,void* sink,void (*emit)(void*, Gfx::FFI::ShapedGlyph const*, size_t, float, size_t, float)){@@ -630,7 +640,8 @@ extern "C" void ladybird_gfx_shape_text_uncached(*static_cast<Gfx::Font const*>(font),text_type,letter_spacing,- word_spacing);+ word_spacing,+ joining_context);emit(sink,reinterpret_cast<Gfx::FFI::ShapedGlyph const*>(shape.glyphs.data()),diff --git a/Libraries/LibUnicode/CharacterTypes.cpp b/Libraries/LibUnicode/CharacterTypes.cppindex ae672c68bea..1cbcd4a210d 100644--- a/Libraries/LibUnicode/CharacterTypes.cpp+++ b/Libraries/LibUnicode/CharacterTypes.cpp@@ -570,6 +570,24 @@ LineBreakClass line_break_class(u32 code_point)}}+JoiningType joining_type(u32 code_point)+{+ switch (static_cast<UJoiningType>(u_getIntPropertyValue(static_cast<UChar32>(code_point), UCHAR_JOINING_TYPE))) {+ case U_JT_TRANSPARENT:+ return JoiningType::Transparent;+ case U_JT_DUAL_JOINING:+ return JoiningType::DualJoining;+ case U_JT_JOIN_CAUSING:+ return JoiningType::JoinCausing;+ case U_JT_LEFT_JOINING:+ return JoiningType::LeftJoining;+ case U_JT_RIGHT_JOINING:+ return JoiningType::RightJoining;+ default:+ return JoiningType::NonJoining;+ }+}+// 22.2.2.7.3 Canonicalize ( rer, ch ), https://tc39.es/ecma262/#sec-runtime-semantics-canonicalize-chu32 canonicalize(u32 code_point, bool unicode_mode){@@ -796,6 +814,7 @@ u32 unicode_get_case_closure(u32, u32*, u32);u8 unicode_layout_bidirectional_class(u32);u8 unicode_layout_line_break_class(u32);+u8 unicode_layout_joining_type(u32);bool unicode_layout_code_point_has_emoji_property(u32);}@@ -990,8 +1009,8 @@ extern "C" u32 unicode_get_case_closure(return count;}-// LibWeb's text chunker classifies code points by these, mirroring the declaration order of BidiClass and-// LineBreakClass.+// LibWeb's text chunker classifies code points by these, mirroring the declaration order of BidiClass,+// LineBreakClass and JoiningType.extern "C" u8 unicode_layout_bidirectional_class(u32 code_point){return to_underlying(Unicode::bidirectional_class(code_point));@@ -1002,6 +1021,11 @@ extern "C" u8 unicode_layout_line_break_class(u32 code_point)return to_underlying(Unicode::line_break_class(code_point));}+extern "C" u8 unicode_layout_joining_type(u32 code_point)+{+ return to_underlying(Unicode::joining_type(code_point));+}+extern "C" bool unicode_layout_code_point_has_emoji_property(u32 code_point){return Unicode::code_point_has_emoji_property(code_point);diff --git a/Libraries/LibUnicode/CharacterTypes.h b/Libraries/LibUnicode/CharacterTypes.hindex 8670fe2ab49..bdfdc6a3de3 100644--- a/Libraries/LibUnicode/CharacterTypes.h+++ b/Libraries/LibUnicode/CharacterTypes.h@@ -96,6 +96,17 @@ enum class LineBreakClass {LineBreakClass line_break_class(u32 code_point);+enum class JoiningType {+ NonJoining, // U+ Transparent, // T+ DualJoining, // D+ JoinCausing, // C+ LeftJoining, // L+ RightJoining, // R+};++JoiningType joining_type(u32 code_point);+struct CodePointRange {u32 from { 0 };u32 to { 0 };diff --git a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rsindex 830265a98d7..e691f0b02fa 100644--- a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs+++ b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs@@ -7,7 +7,7 @@//! Stand-ins for the LibGfx font, shaping and path entry points, which a unit test links through a document's render//! state, without the C++ runtime, but never reaches.-use libgfx_rust::text_layout::{DrawGlyph, ShapedGlyph, TextType};+use libgfx_rust::text_layout::{DrawGlyph, JoiningContext, ShapedGlyph, TextType};use std::ffi::c_void;#[unsafe(no_mangle)]@@ -116,6 +116,7 @@ extern "C" fn ladybird_gfx_shape_text_uncached(_text_type: TextType,_letter_spacing: f32,_word_spacing: f32,+ _joining_context: JoiningContext,_sink: *mut c_void,_emit: unsafe extern "C" fn(*mut c_void, *const ShapedGlyph, usize, f32, usize, f32),) {diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex f5879c4da20..4c9ff26cc32 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -112,6 +112,7 @@ fn glyphs_after_tab_spacer(fn reshaped_glyph_run(text: &[u16],+ range: std::ops::Range<usize>,font: &libgfx_rust::font::FontHandle,text_type: u8,letter_spacing: f32,@@ -119,10 +120,11 @@ fn reshaped_glyph_run() -> OwnedGlyphRun {let shape = libgfx_rust::text_layout::shape_text_shared(font,- text,+ &text[range.clone()],libgfx_rust::text_layout::TextType::try_from(text_type).expect("invalid Gfx::GlyphRun::TextType"),letter_spacing,word_spacing,+ text_chunker::joining_context(text, range),);OwnedGlyphRun {glyphs: shape.glyphs().clone(),@@ -396,7 +398,8 @@ impl Item {.unwrap_or_else(|| {let prepared_item = self.prepared_item(prepared);reshaped_glyph_run(- &text[split_offset_in_node..end],+ text,+ split_offset_in_node..end,prepared.font_of(prepared_item),prepared_item.text_type(),letter_spacing,@@ -552,7 +555,9 @@ impl Item {return None;}- let shape = |text_range: &[u16]| reshaped_glyph_run(text_range, font, text_type, letter_spacing, word_spacing);+ let shape = |range: std::ops::Range<usize>| {+ reshaped_glyph_run(text, range, font, text_type, letter_spacing, word_spacing)+ };let leading_edge_inline_size = self.margin_start + self.border_start + self.padding_start;let prefix_fits = |boundary: usize| {@@ -562,6 +567,7 @@ impl Item {libgfx_rust::text_layout::TextType::try_from(text_type).expect("invalid Gfx::GlyphRun::TextType"),letter_spacing,word_spacing,+ text_chunker::joining_context(text, start..boundary),);leading_edge_inline_size + CssPixels::nearest_value_for_f32(width) <= max_border_box_inline_size};@@ -574,7 +580,7 @@ impl Item {}let split = boundaries[fitting_count - 1];- Some(self.take_prefix_before(split, shape(&text[start..split]), shape(&text[split..end])))+ Some(self.take_prefix_before(split, shape(start..split), shape(split..end)))}pub(crate) fn first_grapheme_cluster_inline_size(@@ -616,6 +622,7 @@ impl Item {libgfx_rust::text_layout::TextType::try_from(run.text_type).expect("invalid Gfx::GlyphRun::TextType"),letter_spacing,word_spacing,+ text_chunker::joining_context(text, start..boundary),);Some(CssPixels::nearest_value_for_f32(width))}@@ -1188,13 +1195,15 @@ impl<'pass> InlineContentPreparer<'pass> {upright_advance: None,});}- let shaped_text = &full_text[shaped_start..shaped_start + shaped_length];+ let shaped_range = shaped_start..shaped_start + shaped_length;+ let shaped_text = &full_text[shaped_range.clone()];let mut shape = libgfx_rust::text_layout::shape_text_shared(font,shaped_text,libgfx_rust::text_layout::TextType::try_from(text_type).expect("invalid Gfx::GlyphRun::TextType"),text_context.letter_spacing,text_context.word_spacing,+ text_chunker::joining_context(full_text, shaped_range),);let mut flags = 0;if !shaped_text.is_empty()diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rsindex fc13785230d..7b2068ba80a 100644--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs@@ -163,6 +163,7 @@ unsafe extern "C" {);safe fn unicode_layout_bidirectional_class(code_point: u32) -> u8;safe fn unicode_layout_line_break_class(code_point: u32) -> u8;+ safe fn unicode_layout_joining_type(code_point: u32) -> u8;safe fn unicode_layout_code_point_has_emoji_property(code_point: u32) -> bool;}@@ -189,6 +190,68 @@ fn line_break_class(code_point: u32) -> u8 {unicode_layout_line_break_class(code_point)}+// LibUnicode's JoiningType, in declaration order.+mod joining_type {+ pub(super) const NON_JOINING: u8 = 0;+ pub(super) const TRANSPARENT: u8 = 1;+ pub(super) const DUAL_JOINING: u8 = 2;+ pub(super) const JOIN_CAUSING: u8 = 3;+ pub(super) const LEFT_JOINING: u8 = 4;+ pub(super) const RIGHT_JOINING: u8 = 5;+}++fn joining_type(code_point: u32) -> u8 {+ // OPTIMIZATION: Below U+0300, only U+00AD SOFT HYPHEN takes part in joining, as a transparent character.+ if code_point < 0x300 && code_point != 0xad {+ return joining_type::NON_JOINING;+ }+ unicode_layout_joining_type(code_point)+}++/// The joining type of the first character in `text[start..end]` that is not transparent to joining, such as most marks+/// are, which shaping looks past for the letter that a letter joins.+fn first_joining_type_in(text: &[u16], start: usize, end: usize) -> u8 {+ let mut index = start;+ while index < end {+ let code_point = code_point_at(text, index);+ let joining_type = joining_type(code_point);+ if joining_type != joining_type::TRANSPARENT {+ return joining_type;+ }+ index += code_unit_length_for_code_point(code_point);+ }+ joining_type::NON_JOINING+}++/// Like `first_joining_type_in()`, for the last such character.+fn last_joining_type_in(text: &[u16], start: usize, end: usize) -> u8 {+ let mut index = end;+ while index > start {+ let joining_type = joining_type(previous_code_point_at(text, &mut index));+ if joining_type != joining_type::TRANSPARENT {+ return joining_type;+ }+ }+ joining_type::NON_JOINING+}++/// Whether the letters at the edges of `range` join the letters of `text` around it, so that shaping the range on its+/// own gives them the forms they take in the whole text.+pub(crate) fn joining_context(text: &[u16], range: std::ops::Range<usize>) -> libgfx_rust::text_layout::JoiningContext {+ use joining_type::*;+ if range.is_empty() {+ return Default::default();+ }+ let joins_following = |joining_type| matches!(joining_type, DUAL_JOINING | JOIN_CAUSING | LEFT_JOINING);+ let joins_preceding = |joining_type| matches!(joining_type, DUAL_JOINING | JOIN_CAUSING | RIGHT_JOINING);+ libgfx_rust::text_layout::JoiningContext {+ joins_before: joins_preceding(first_joining_type_in(text, range.start, range.end))+ && joins_following(last_joining_type_in(text, 0, range.start)),+ joins_after: joins_following(last_joining_type_in(text, range.start, range.end))+ && joins_preceding(first_joining_type_in(text, range.end, text.len())),+ }+}+// Classifies a code point for direction-run splitting: strong LTR/RTL, direction-neutral Common, or ContextDependent// (resolved from surrounding runs).fn text_type_for_code_point(code_point: u32) -> u8 {@@ -300,6 +363,11 @@ mod unicode_test_stubs {unreachable!("no unit test segments text");}+ #[unsafe(no_mangle)]+ extern "C" fn unicode_layout_joining_type(_code_point: u32) -> u8 {+ unreachable!("no unit test shapes text");+ }+#[unsafe(no_mangle)]extern "C" fn unicode_layout_code_point_has_emoji_property(_code_point: u32) -> bool {unreachable!("no unit test segments text");diff --git a/Tests/LibWeb/Assets/NotoSansArabic-yeh-subset.ttf b/Tests/LibWeb/Assets/NotoSansArabic-yeh-subset.ttfindex c31785ce82f..3e3270c2fb4 100644Binary files a/Tests/LibWeb/Assets/NotoSansArabic-yeh-subset.ttf and b/Tests/LibWeb/Assets/NotoSansArabic-yeh-subset.ttf differdiff --git a/Tests/LibWeb/Ref/expected/css/word-break-break-all-arabic-joining-ref.html b/Tests/LibWeb/Ref/expected/css/word-break-break-all-arabic-joining-ref.htmlnew file mode 100644index 00000000000..7ab4fe50779--- /dev/null+++ b/Tests/LibWeb/Ref/expected/css/word-break-break-all-arabic-joining-ref.html@@ -0,0 +1,13 @@+<!DOCTYPE html>+<style>+ @font-face {+ font-family: Yeh;+ src: url("../../../Assets/NotoSansArabic-yeh-subset.ttf");+ }+ div {+ font: 40px Yeh;+ }+</style>+<body dir="rtl">+<div>ىىى</div>+<div>ى‍<br>‍ى‍<br>‍ى</div>diff --git a/Tests/LibWeb/Ref/input/css/word-break-break-all-arabic-joining.html b/Tests/LibWeb/Ref/input/css/word-break-break-all-arabic-joining.htmlnew file mode 100644index 00000000000..1ffbab66105--- /dev/null+++ b/Tests/LibWeb/Ref/input/css/word-break-break-all-arabic-joining.html@@ -0,0 +1,15 @@+<!DOCTYPE html>+<link rel="match" href="../../expected/css/word-break-break-all-arabic-joining-ref.html" />+<style>+ @font-face {+ font-family: Yeh;+ src: url("../../../Assets/NotoSansArabic-yeh-subset.ttf");+ }+ div {+ font: 40px Yeh;+ word-break: break-all;+ }+</style>+<body dir="rtl">+<div>ىىى</div>+<div style="width: 0">ىىى</div>