Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/stub_ui/lib/src/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,8 @@ enum BoxHeightStyle {
/// entire paragraph. The top edge of each line will align with the bottom
/// edge of the previous line. It is possible for glyphs to extend outside
/// these boxes.
///
/// Will fall back to tight bounds if the strut is disabled or invalid.
strut,
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,8 @@ enum BoxHeightStyle {
/// entire paragraph. The top edge of each line will align with the bottom
/// edge of the previous line. It is possible for glyphs to extend outside
/// these boxes.
///
/// Will fall back to tight bounds if the strut is disabled or invalid.
strut,
}

Expand Down
33 changes: 21 additions & 12 deletions third_party/txt/src/txt/paragraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ bool Paragraph::ComputeBidiRuns(std::vector<BidiRun>* result) {
return true;
}

bool Paragraph::IsStrutValid() const {
// Font size must be positive.
return (paragraph_style_.strut_enabled &&
paragraph_style_.strut_font_size >= 0);
}

void Paragraph::ComputeStrut(StrutMetrics* strut, SkFont& font) {
strut->ascent = 0;
strut->descent = 0;
Expand All @@ -433,15 +439,12 @@ void Paragraph::ComputeStrut(StrutMetrics* strut, SkFont& font) {
strut->line_height = 0;
strut->force_strut = false;

// Font size must be positive.
bool valid_strut =
paragraph_style_.strut_enabled && paragraph_style_.strut_font_size >= 0;
if (!valid_strut) {
if (!IsStrutValid())
return;
}

// force_strut makes all lines have exactly the strut metrics, and ignores all
// actual metrics. We only force the strut if the strut is non-zero and valid.
strut->force_strut = paragraph_style_.force_strut_height && valid_strut;
strut->force_strut = paragraph_style_.force_strut_height;
minikin::FontStyle minikin_font_style(
0, GetWeight(paragraph_style_.strut_font_weight),
paragraph_style_.strut_font_style == FontStyle::italic);
Expand Down Expand Up @@ -1458,12 +1461,18 @@ std::vector<Paragraph::TextBox> Paragraph::GetRectsForRange(
box.direction);
}
} else if (rect_height_style == RectHeightStyle::kStrut) {
for (const Paragraph::TextBox& box : kv.second.boxes) {
boxes.emplace_back(
SkRect::MakeLTRB(
box.rect.fLeft, line_baselines_[kv.first] - strut_.ascent,
box.rect.fRight, line_baselines_[kv.first] + strut_.descent),
box.direction);
if (IsStrutValid()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document this fallback behavior here as well as in the enum declaration?

for (const Paragraph::TextBox& box : kv.second.boxes) {
boxes.emplace_back(
SkRect::MakeLTRB(
box.rect.fLeft, line_baselines_[kv.first] - strut_.ascent,
box.rect.fRight, line_baselines_[kv.first] + strut_.descent),
box.direction);
}
} else {
// Fall back to tight bounds if the strut is invalid.
boxes.insert(boxes.end(), kv.second.boxes.begin(),
kv.second.boxes.end());
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions third_party/txt/src/txt/paragraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ class Paragraph {
// Calculates and populates strut based on paragraph_style_ strut info.
void ComputeStrut(StrutMetrics* strut, SkFont& font);

bool IsStrutValid() const;

// Calculate the starting X offset of a line based on the line's width and
// alignment.
double GetLineXOffset(double line_total_advance);
Expand Down
35 changes: 35 additions & 0 deletions third_party/txt/tests/paragraph_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,41 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeStrut)) {
ASSERT_TRUE(Snapshot());
}

TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeStrutFallback)) {
const char* text = "Chinese 字典";

auto icu_text = icu::UnicodeString::fromUTF8(text);
std::u16string u16_text(icu_text.getBuffer(),
icu_text.getBuffer() + icu_text.length());

txt::ParagraphStyle paragraph_style;
paragraph_style.strut_enabled = false;
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());

txt::TextStyle text_style;
text_style.font_families.push_back("Noto Sans CJK JP");
text_style.font_size = 20;
builder.PushStyle(text_style);

builder.AddText(u16_text);

builder.Pop();

auto paragraph = builder.Build();
paragraph->Layout(550);

std::vector<txt::Paragraph::TextBox> strut_boxes =
paragraph->GetRectsForRange(0, 10, Paragraph::RectHeightStyle::kStrut,
Paragraph::RectWidthStyle::kMax);
std::vector<txt::Paragraph::TextBox> tight_boxes =
paragraph->GetRectsForRange(0, 10, Paragraph::RectHeightStyle::kTight,
Paragraph::RectWidthStyle::kMax);

ASSERT_EQ(strut_boxes.size(), 1ull);
ASSERT_EQ(tight_boxes.size(), 1ull);
ASSERT_EQ(strut_boxes.front().rect, tight_boxes.front().rect);
}

SkRect GetCoordinatesForGlyphPosition(const txt::Paragraph& paragraph,
size_t pos) {
std::vector<txt::Paragraph::TextBox> boxes =
Expand Down