Skip to content

Commit 9e2fa55

Browse files
author
ci_lynx
committed
[BugFix][Text] Reland: Allow auto font size when max size is unset
Fix early-return in 'TextRender::HandleAutoSize' that incorrectly disabled auto font sizing when 'auto_font_size_max_size_' was unset or less than 'auto_font_size_min_size_'. Auto sizing now only short-circuits on invalid 'auto_font_size_step_granularity_'. Add a unit test to ensure auto font size can shrink text when max size is unset. cherry-pick from: cd17fa35f85a326490442aded6e36f0c08d87357 issue: m-7335486560
1 parent 53e6041 commit 9e2fa55

2 files changed

Lines changed: 126 additions & 11 deletions

File tree

clay/ui/shadow/text_render.cc

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ namespace utils = attribute_utils;
4747
constexpr float kLayoutTolerance = 1.f;
4848
static constexpr float kDefaultFontSizeInDip = 14.f;
4949

50+
static bool HasInlineTruncationShadowNode(ShadowNode* node) {
51+
if (!node) {
52+
return false;
53+
}
54+
for (auto* child : node->GetChildren()) {
55+
if (child->IsInlineTruncationShadowNode() ||
56+
HasInlineTruncationShadowNode(child)) {
57+
return true;
58+
}
59+
}
60+
return false;
61+
}
62+
5063
static bool InlineTruncationTextBoxFits(
5164
const txt::Paragraph::TextBox& box, double layout_width,
5265
double visible_bottom, double target_line_top, double target_line_bottom,
@@ -554,6 +567,14 @@ float TextRender::GetMaxFontSize() {
554567
void TextRender::HandleAutoSize(const MeasureConstraint& constraint,
555568
ShadowLayoutContextMeasure* context) {
556569
if (measure_node_->enable_auto_font_size_) {
570+
if (HasInlineTruncationShadowNode(measure_node_)) {
571+
return;
572+
}
573+
if (measure_node_->auto_font_size_preset_sizes_.empty() &&
574+
(measure_node_->auto_font_size_step_granularity_ <= 0 ||
575+
measure_node_->auto_font_size_min_size_ <= 0)) {
576+
return;
577+
}
557578
if (!CheckTextFullyDisplayed(constraint, context)) {
558579
TryShrinkFontSize(constraint, context);
559580
} else {
@@ -619,19 +640,24 @@ void TextRender::TryShrinkFontSize(
619640
}
620641
while (measure_node_->text_style_->font_size >=
621642
measure_node_->auto_font_size_min_size_) {
622-
measure_node_->text_style_->font_size =
623-
measure_node_->text_style_->font_size.value_or(
624-
kDefaultFontSizeInDip * measure_node_->Logical2ClayPixelRatio()) -
625-
measure_node_->auto_font_size_step_granularity_;
643+
auto current_font_size = measure_node_->text_style_->font_size.value_or(
644+
kDefaultFontSizeInDip * measure_node_->Logical2ClayPixelRatio());
645+
auto target_font_size = std::max(
646+
current_font_size - measure_node_->auto_font_size_step_granularity_,
647+
measure_node_->auto_font_size_min_size_);
648+
if (target_font_size == current_font_size) {
649+
return;
650+
}
651+
measure_node_->text_style_->font_size = target_font_size;
626652
FlexInlineFontSize(true, measure_node_->text_style_->font_size.value(),
627653
measure_node_);
628-
if (measure_node_->text_style_->font_size >=
629-
measure_node_->auto_font_size_min_size_) {
630-
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
631-
BuildTextLayout(constraint, context_measure);
632-
if (CheckTextFullyDisplayed(constraint, context_measure)) {
633-
return;
634-
}
654+
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
655+
BuildTextLayout(constraint, context_measure);
656+
if (CheckTextFullyDisplayed(constraint, context_measure)) {
657+
return;
658+
}
659+
if (target_font_size == measure_node_->auto_font_size_min_size_) {
660+
return;
635661
}
636662
}
637663
}

clay/ui/shadow/text_unittests.cc

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,95 @@ TEST_F_UI(TextTest, AutoFontSizeStepGranularity) {
6363
EXPECT_NE(text_shadow_node_->text_style_->font_size, font_size);
6464
}
6565

66+
TEST_F_UI(TextTest, AutoFontSizeIgnoresInvalidStepGranularity) {
67+
MeasureConstraint constraint{4000, MeasureMode::kDefinite, 1000,
68+
MeasureMode::kDefinite};
69+
text_shadow_node_->enable_auto_font_size_ = true;
70+
text_shadow_node_->auto_font_size_max_size_ = 50;
71+
text_shadow_node_->auto_font_size_min_size_ = 30;
72+
text_shadow_node_->auto_font_size_step_granularity_ = 0;
73+
raw_text_shadow_node_->SetText("Hello");
74+
75+
text_shadow_node_->Measure(constraint);
76+
77+
EXPECT_EQ(text_shadow_node_->text_style_->font_size, 42);
78+
}
79+
80+
TEST_F_UI(TextTest, AutoFontSizeAllowsUnsetMaxSize) {
81+
MeasureConstraint constraint{1000, MeasureMode::kDefinite, 100,
82+
MeasureMode::kDefinite};
83+
text_shadow_node_->enable_auto_font_size_ = true;
84+
text_shadow_node_->auto_font_size_min_size_ = 10;
85+
text_shadow_node_->auto_font_size_max_size_ = 0;
86+
text_shadow_node_->auto_font_size_step_granularity_ = 1;
87+
raw_text_shadow_node_->SetText(
88+
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
89+
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");
90+
91+
text_shadow_node_->Measure(constraint);
92+
93+
EXPECT_LT(text_shadow_node_->text_style_->font_size, 42);
94+
}
95+
96+
TEST_F_UI(TextTest, AutoFontSizeIgnoresUnsetMinSize) {
97+
MeasureConstraint constraint{100, MeasureMode::kDefinite, 1,
98+
MeasureMode::kDefinite};
99+
text_shadow_node_->enable_auto_font_size_ = true;
100+
text_shadow_node_->auto_font_size_min_size_ = 0;
101+
text_shadow_node_->auto_font_size_max_size_ = 0;
102+
text_shadow_node_->auto_font_size_step_granularity_ = 1;
103+
raw_text_shadow_node_->SetText(
104+
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
105+
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");
106+
107+
text_shadow_node_->Measure(constraint);
108+
109+
EXPECT_EQ(text_shadow_node_->text_style_->font_size, 42);
110+
}
111+
112+
TEST_F_UI(TextTest, AutoFontSizeDoesNotShrinkBelowMinSize) {
113+
MeasureConstraint constraint{100, MeasureMode::kDefinite, 1,
114+
MeasureMode::kDefinite};
115+
text_shadow_node_->enable_auto_font_size_ = true;
116+
text_shadow_node_->auto_font_size_min_size_ = 10;
117+
text_shadow_node_->auto_font_size_max_size_ = 0;
118+
text_shadow_node_->auto_font_size_step_granularity_ = 3;
119+
raw_text_shadow_node_->SetText(
120+
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
121+
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");
122+
123+
text_shadow_node_->Measure(constraint);
124+
125+
EXPECT_GE(text_shadow_node_->text_style_->font_size, 10);
126+
}
127+
128+
TEST_F_UI(TextTest, AutoFontSizeIgnoresInlineTruncation) {
129+
MeasureConstraint constraint{1000, MeasureMode::kDefinite, 100,
130+
MeasureMode::kDefinite};
131+
text_shadow_node_->enable_auto_font_size_ = true;
132+
text_shadow_node_->auto_font_size_min_size_ = 10;
133+
text_shadow_node_->auto_font_size_max_size_ = 0;
134+
text_shadow_node_->auto_font_size_step_granularity_ = 1;
135+
text_shadow_node_->SetTextMaxLine(1);
136+
raw_text_shadow_node_->SetText(
137+
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
138+
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");
139+
auto inline_truncation_node = std::make_unique<InlineTruncationShadowNode>(
140+
owner_, std::string("inline-truncation"), -1);
141+
auto inline_text_node = std::make_unique<InlineTextShadowNode>(
142+
owner_, std::string("inline-text"), -1);
143+
auto inline_raw_text_node =
144+
std::make_unique<RawTextShadowNode>(owner_, std::string("raw-text"), -1);
145+
inline_raw_text_node->SetText("...");
146+
inline_text_node->AddChild(inline_raw_text_node.get());
147+
inline_truncation_node->AddChild(inline_text_node.get());
148+
text_shadow_node_->AddChild(inline_truncation_node.get());
149+
150+
text_shadow_node_->Measure(constraint);
151+
152+
EXPECT_EQ(text_shadow_node_->text_style_->font_size, 42);
153+
}
154+
66155
TEST_F_UI(TextTest, DISABLED_AutoFontSizePreset) {
67156
MeasureConstraint constraint{1000, MeasureMode::kDefinite, 100,
68157
MeasureMode::kDefinite};

0 commit comments

Comments
 (0)