Skip to content
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
48 changes: 37 additions & 11 deletions clay/ui/shadow/text_render.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ namespace utils = attribute_utils;
constexpr float kLayoutTolerance = 1.f;
static constexpr float kDefaultFontSizeInDip = 14.f;

static bool HasInlineTruncationShadowNode(ShadowNode* node) {
if (!node) {
return false;
}
for (auto* child : node->GetChildren()) {
if (child->IsInlineTruncationShadowNode() ||
HasInlineTruncationShadowNode(child)) {
return true;
}
}
return false;
}

static bool InlineTruncationTextBoxFits(
const txt::Paragraph::TextBox& box, double layout_width,
double visible_bottom, double target_line_top, double target_line_bottom,
Expand Down Expand Up @@ -554,6 +567,14 @@ float TextRender::GetMaxFontSize() {
void TextRender::HandleAutoSize(const MeasureConstraint& constraint,
ShadowLayoutContextMeasure* context) {
if (measure_node_->enable_auto_font_size_) {
if (HasInlineTruncationShadowNode(measure_node_)) {
return;
}
if (measure_node_->auto_font_size_preset_sizes_.empty() &&
(measure_node_->auto_font_size_step_granularity_ <= 0 ||
measure_node_->auto_font_size_min_size_ <= 0)) {
return;
}
if (!CheckTextFullyDisplayed(constraint, context)) {
TryShrinkFontSize(constraint, context);
} else {
Expand Down Expand Up @@ -619,19 +640,24 @@ void TextRender::TryShrinkFontSize(
}
while (measure_node_->text_style_->font_size >=
measure_node_->auto_font_size_min_size_) {
measure_node_->text_style_->font_size =
measure_node_->text_style_->font_size.value_or(
kDefaultFontSizeInDip * measure_node_->Logical2ClayPixelRatio()) -
measure_node_->auto_font_size_step_granularity_;
auto current_font_size = measure_node_->text_style_->font_size.value_or(
kDefaultFontSizeInDip * measure_node_->Logical2ClayPixelRatio());
auto target_font_size = std::max(
current_font_size - measure_node_->auto_font_size_step_granularity_,
measure_node_->auto_font_size_min_size_);
if (target_font_size == current_font_size) {
return;
}
measure_node_->text_style_->font_size = target_font_size;
FlexInlineFontSize(true, measure_node_->text_style_->font_size.value(),
measure_node_);
if (measure_node_->text_style_->font_size >=
measure_node_->auto_font_size_min_size_) {
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
BuildTextLayout(constraint, context_measure);
if (CheckTextFullyDisplayed(constraint, context_measure)) {
return;
}
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
BuildTextLayout(constraint, context_measure);
if (CheckTextFullyDisplayed(constraint, context_measure)) {
return;
}
if (target_font_size == measure_node_->auto_font_size_min_size_) {
return;
}
}
}
Expand Down
89 changes: 89 additions & 0 deletions clay/ui/shadow/text_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,95 @@ TEST_F_UI(TextTest, AutoFontSizeStepGranularity) {
EXPECT_NE(text_shadow_node_->text_style_->font_size, font_size);
}

TEST_F_UI(TextTest, AutoFontSizeIgnoresInvalidStepGranularity) {
MeasureConstraint constraint{4000, MeasureMode::kDefinite, 1000,
MeasureMode::kDefinite};
text_shadow_node_->enable_auto_font_size_ = true;
text_shadow_node_->auto_font_size_max_size_ = 50;
text_shadow_node_->auto_font_size_min_size_ = 30;
text_shadow_node_->auto_font_size_step_granularity_ = 0;
raw_text_shadow_node_->SetText("Hello");

text_shadow_node_->Measure(constraint);

EXPECT_EQ(text_shadow_node_->text_style_->font_size, 42);
}

TEST_F_UI(TextTest, AutoFontSizeAllowsUnsetMaxSize) {
MeasureConstraint constraint{1000, MeasureMode::kDefinite, 100,
MeasureMode::kDefinite};
text_shadow_node_->enable_auto_font_size_ = true;
text_shadow_node_->auto_font_size_min_size_ = 10;
text_shadow_node_->auto_font_size_max_size_ = 0;
text_shadow_node_->auto_font_size_step_granularity_ = 1;
raw_text_shadow_node_->SetText(
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");

text_shadow_node_->Measure(constraint);

EXPECT_LT(text_shadow_node_->text_style_->font_size, 42);
}

TEST_F_UI(TextTest, AutoFontSizeIgnoresUnsetMinSize) {
MeasureConstraint constraint{100, MeasureMode::kDefinite, 1,
MeasureMode::kDefinite};
text_shadow_node_->enable_auto_font_size_ = true;
text_shadow_node_->auto_font_size_min_size_ = 0;
text_shadow_node_->auto_font_size_max_size_ = 0;
text_shadow_node_->auto_font_size_step_granularity_ = 1;
raw_text_shadow_node_->SetText(
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");

text_shadow_node_->Measure(constraint);

EXPECT_EQ(text_shadow_node_->text_style_->font_size, 42);
}

TEST_F_UI(TextTest, AutoFontSizeDoesNotShrinkBelowMinSize) {
MeasureConstraint constraint{100, MeasureMode::kDefinite, 1,
MeasureMode::kDefinite};
text_shadow_node_->enable_auto_font_size_ = true;
text_shadow_node_->auto_font_size_min_size_ = 10;
text_shadow_node_->auto_font_size_max_size_ = 0;
text_shadow_node_->auto_font_size_step_granularity_ = 3;
raw_text_shadow_node_->SetText(
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");

text_shadow_node_->Measure(constraint);

EXPECT_GE(text_shadow_node_->text_style_->font_size, 10);
}

TEST_F_UI(TextTest, AutoFontSizeIgnoresInlineTruncation) {
MeasureConstraint constraint{1000, MeasureMode::kDefinite, 100,
MeasureMode::kDefinite};
text_shadow_node_->enable_auto_font_size_ = true;
text_shadow_node_->auto_font_size_min_size_ = 10;
text_shadow_node_->auto_font_size_max_size_ = 0;
text_shadow_node_->auto_font_size_step_granularity_ = 1;
text_shadow_node_->SetTextMaxLine(1);
raw_text_shadow_node_->SetText(
"Hello, Compiler NG Hello, Compiler NG Hello, Compiler NG Hello, "
"Compiler NG Hello, Compiler NG Hello, Compiler NG ");
auto inline_truncation_node = std::make_unique<InlineTruncationShadowNode>(
owner_, std::string("inline-truncation"), -1);
auto inline_text_node = std::make_unique<InlineTextShadowNode>(
owner_, std::string("inline-text"), -1);
auto inline_raw_text_node =
std::make_unique<RawTextShadowNode>(owner_, std::string("raw-text"), -1);
inline_raw_text_node->SetText("...");
inline_text_node->AddChild(inline_raw_text_node.get());
inline_truncation_node->AddChild(inline_text_node.get());
text_shadow_node_->AddChild(inline_truncation_node.get());

text_shadow_node_->Measure(constraint);

EXPECT_EQ(text_shadow_node_->text_style_->font_size, 42);
}

TEST_F_UI(TextTest, DISABLED_AutoFontSizePreset) {
MeasureConstraint constraint{1000, MeasureMode::kDefinite, 100,
MeasureMode::kDefinite};
Expand Down
Loading