diff --git a/third_party/blink/renderer/core/layout/inline/inline_node.cc b/third_party/blink/renderer/core/layout/inline/inline_node.cc index ced83a6e69c75..598143f4843b5 100644 --- a/third_party/blink/renderer/core/layout/inline/inline_node.cc +++ b/third_party/blink/renderer/core/layout/inline/inline_node.cc @@ -2274,9 +2274,10 @@ static LayoutUnit ComputeContentSize(InlineNode node, // fragment (line) that we cannot compute max-content from // min-content. !line_breaker.HasClonedBoxDecorations() && - !line_info.MayHaveRubyOverhang(); - if (can_compute_max_size_from_min_size) + !line_info.MayHaveRubyOverhang() && !line_info.HasTrailingSpaces(); + if (can_compute_max_size_from_min_size) { max_size_from_min_size.ComputeFromMinSize(line_info); + } } else { result = floats_max_size.ComputeMaxSizeForLine(inline_size, result); } diff --git a/third_party/blink/renderer/core/layout/inline/line_breaker.cc b/third_party/blink/renderer/core/layout/inline/line_breaker.cc index 8fb06e89bc0f8..0fdeeb3ff3370 100644 --- a/third_party/blink/renderer/core/layout/inline/line_breaker.cc +++ b/third_party/blink/renderer/core/layout/inline/line_breaker.cc @@ -1171,6 +1171,11 @@ void LineBreaker::ComputeLineLocation(LineInfo* line_info) const { {line_opportunity_.line_left_offset, line_opportunity_.bfc_block_offset}); if (mode_ == LineBreakerMode::kContent) { line_info->UpdateTextAlign(); + } else if (line_info->HasTrailingSpaces()) { + line_info->SetWidth( + line_info->AvailableWidth(), + (line_info->Width() - line_info->ComputeTrailingSpaceWidth()) + .ClampNegativeToZero()); } } @@ -2006,7 +2011,7 @@ bool LineBreaker::HandleTextForFastMinContent(InlineItemResult* item_result, non_hangable_run_end = next_break; if (!should_break_spaces) { while (non_hangable_run_end > start_offset && - IsBreakableSpace(text[non_hangable_run_end - 1])) { + IsBreakableSpaceOrOtherSeparator(text[non_hangable_run_end - 1])) { --non_hangable_run_end; } } @@ -2046,7 +2051,9 @@ bool LineBreaker::HandleTextForFastMinContent(InlineItemResult* item_result, // Create an `InlineItemResult` that has the max of widths of all words. DCHECK_GE(non_hangable_run_end, item_result->StartOffset()); DCHECK_LE(non_hangable_run_end, item_end_offset); - if (item_style.ShouldCollapseWhiteSpaces()) { + if (item_style.ShouldCollapseWhiteSpaces() && + (non_hangable_run_end == next_break || + !unicode::IsOtherSpaceSeparator(text[next_break - 1]))) { item_result->text_offset.end = non_hangable_run_end; trailing_whitespace_ = non_hangable_run_end != next_break ? WhitespaceState::kCollapsed @@ -2853,7 +2860,8 @@ void LineBreaker::SplitTrailingBidiPreservedSpace(LineInfo* line_info) { wtf_size_t i = item_result.EndOffset(); for (; i > item_result.StartOffset() && - (IsBreakableSpace(text[i - 1]) || IsBidiTrailingSpace(text[i - 1])); + (IsBreakableSpaceOrOtherSeparator(text[i - 1]) || + IsBidiTrailingSpace(text[i - 1])); i--) { } diff --git a/third_party/blink/renderer/core/layout/inline/line_info.cc b/third_party/blink/renderer/core/layout/inline/line_info.cc index dd93cdaeca105..68950ebee5c27 100644 --- a/third_party/blink/renderer/core/layout/inline/line_info.cc +++ b/third_party/blink/renderer/core/layout/inline/line_info.cc @@ -327,6 +327,9 @@ LayoutUnit LineInfo::ComputeTrailingSpaceWidth(unsigned* end_offset_out) const { DCHECK(!item_result.inline_size); continue; // Skip empty items. See `LineBreaker::HandleEmptyText`. } + if (!item_result.shape_result) { + continue; + } const String& text = items_data_->text_content; DCHECK_GE(end_offset, 1u); diff --git a/third_party/blink/renderer/core/layout/inline/line_info.h b/third_party/blink/renderer/core/layout/inline/line_info.h index b33f65d37f838..93ae4950a9d3b 100644 --- a/third_party/blink/renderer/core/layout/inline/line_info.h +++ b/third_party/blink/renderer/core/layout/inline/line_info.h @@ -170,6 +170,10 @@ class CORE_EXPORT LineInfo { // without clamping. LayoutUnit ComputeWidth() const; + // The width of preserved trailing spaces that hang. + LayoutUnit ComputeTrailingSpaceWidth( + unsigned* end_offset_out = nullptr) const; + bool HasTrailingSpaces() const { return has_trailing_spaces_; } void SetHasTrailingSpaces() { has_trailing_spaces_ = true; } @@ -291,9 +295,6 @@ class CORE_EXPORT LineInfo { ETextAlign GetTextAlign(bool is_last_line = false) const; bool ComputeNeedsAccurateEndPosition() const; - // The width of preserved trailing spaces. - LayoutUnit ComputeTrailingSpaceWidth( - unsigned* end_offset_out = nullptr) const; unsigned InflowEndOffsetInternal(bool skip_forced_break) const; Member items_data_; diff --git a/third_party/blink/renderer/platform/fonts/shaping/shaping_line_breaker.cc b/third_party/blink/renderer/platform/fonts/shaping/shaping_line_breaker.cc index 4675b1b59597e..62493d8229a3f 100644 --- a/third_party/blink/renderer/platform/fonts/shaping/shaping_line_breaker.cc +++ b/third_party/blink/renderer/platform/fonts/shaping/shaping_line_breaker.cc @@ -452,7 +452,9 @@ const ShapeResultView* ShapingLineBreaker::ShapeLine( DCHECK(!is_break_after_any_space); DCHECK(IsBreakableSpace(text[candidate_break])); if (break_opportunity.non_hangable_run_end && - break_opportunity.non_hangable_run_end <= start) { + break_opportunity.non_hangable_run_end <= start && + IsAllSpaces(text, start, + std::min(range_end, break_opportunity.offset))) { // TODO (jfenandez): There may be cases where candidate_break is // not a breakable space but we also want to early return for // triggering the trailing spaces handling diff --git a/third_party/blink/renderer/platform/text/character.h b/third_party/blink/renderer/platform/text/character.h index 3d40c67f8ca74..e1323d4b77fc4 100644 --- a/third_party/blink/renderer/platform/text/character.h +++ b/third_party/blink/renderer/platform/text/character.h @@ -183,9 +183,20 @@ inline bool IsCollapsibleSpace(UChar c) { inline bool IsLineFeed(UChar c) { return c == uchar::kLineFeed; } +// CSS Text Level 3 "other space separators": all characters in the Unicode Zs +// (Space_Separator) category except SPACE (U+0020) and NO-BREAK SPACE (U+00A0). +// https://drafts.csswg.org/css-text-3/#other-space-separators template inline bool IsOtherSpaceSeparator(CharacterType c) { - return c == uchar::kIdeographicSpace; + return c == uchar::kOghamSpaceMark || + (c >= uchar::kEnQuad && c <= uchar::kHairSpace) || + c == uchar::kNarrowNoBreakSpace || + c == uchar::kMediumMathematicalSpace || c == uchar::kIdeographicSpace; +} +template +inline bool IsBreakableOtherSpaceSeparator(CharacterType c) { + return IsOtherSpaceSeparator(c) && c != uchar::kFigureSpace && + c != uchar::kNarrowNoBreakSpace; } inline bool TreatAsSpace(UChar32 c) { return c == uchar::kSpace || c == uchar::kTab || c == uchar::kLineFeed || diff --git a/third_party/blink/renderer/platform/text/text_break_iterator.cc b/third_party/blink/renderer/platform/text/text_break_iterator.cc index 72ed77ba5fdf4..ae12690c71b64 100644 --- a/third_party/blink/renderer/platform/text/text_break_iterator.cc +++ b/third_party/blink/renderer/platform/text/text_break_iterator.cc @@ -310,11 +310,11 @@ inline wtf_size_t LazyLineBreakIterator::NextBreakablePosition( break; case BreakSpaceType::kAfterEverySpace: if (context.last.is_space || - unicode::IsOtherSpaceSeparator(context.last.ch)) { + unicode::IsBreakableOtherSpaceSeparator(context.last.ch)) { return i; } if ((context.current.is_space || - unicode::IsOtherSpaceSeparator(context.current.ch)) && + unicode::IsBreakableOtherSpaceSeparator(context.current.ch)) && i + 1 < len) { return i + 1; } diff --git a/third_party/blink/renderer/platform/wtf/text/character_names.h b/third_party/blink/renderer/platform/wtf/text/character_names.h index 78ccb00d620d3..4ea242eae1f6c 100644 --- a/third_party/blink/renderer/platform/wtf/text/character_names.h +++ b/third_party/blink/renderer/platform/wtf/text/character_names.h @@ -107,11 +107,14 @@ const UChar kEthiopicWordspace = 0x1361; const UChar kEthiopicPrefaceColon = 0x1366; const UChar kEthiopicNumberHundred = 0x137B; const UChar kEthiopicNumberTenThousand = 0x137C; +const UChar kOghamSpaceMark = 0x1680; const UChar kMongolianFreeVariationSelectorTwo = 0x180C; const UChar kMongolianLetterA = 0x1820; // U+2*** const UChar kEnQuad = 0x2000; +const UChar kFigureSpace = 0x2007; +const UChar kHairSpace = 0x200A; const UChar kZeroWidthSpace = 0x200B; const UChar kZeroWidthNonJoiner = 0x200C; const UChar kZeroWidthJoiner = 0x200D; @@ -134,12 +137,14 @@ const UChar kRightToLeftEmbedding = 0x202B; const UChar kPopDirectionalFormatting = 0x202C; const UChar kLeftToRightOverride = 0x202D; const UChar kRightToLeftOverride = 0x202E; +const UChar kNarrowNoBreakSpace = 0x202F; const UChar kPerMilleSign = 0x2030; const UChar kPerTenThousandSign = 0x2031; const UChar kOverline = 0x203E; const UChar kTironianSignEt = 0x204A; const UChar kReversedPilcrowSign = 0x204B; const UChar kSwungDash = 0x2053; +const UChar kMediumMathematicalSpace = 0x205F; const UChar kLeftToRightIsolate = 0x2066; const UChar kRightToLeftIsolate = 0x2067; const UChar kFirstStrongIsolate = 0x2068;