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 2 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
10 changes: 8 additions & 2 deletions impeller/entity/contents/text_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ TextContents::TextContents() = default;

TextContents::~TextContents() = default;

// static
Scalar TextContents::RoundFontScale(Scalar value) {
return std::round(value * 10.0) / 10.0;

@bdero bdero Jul 18, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this snapping would need to be done on a logarithmic scale (apply log, apply snapping, invert log w/ exponential)? Also, the snapping precision would need to somehow incorporate the text frame's font size as well.

For example, the difference between 0.1 and 0.2 is potentially huge, getting worse as the given font size increases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm thats a good point. I'll need to investigate this a bit more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay, after much thinking I've come up with an OK solution.... round to one decimal if the point size is < 25, Round to two decimals if > 50. 🤷‍♂️

}

void TextContents::SetTextFrame(const TextFrame& frame) {
frame_ = frame;
}
Expand Down Expand Up @@ -78,8 +83,9 @@ std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
void TextContents::PopulateGlyphAtlas(
const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
Scalar scale) {
lazy_glyph_atlas->AddTextFrame(frame_, scale);
scale_ = scale;
auto rounded = RoundFontScale(scale);
lazy_glyph_atlas->AddTextFrame(frame_, rounded);
scale_ = rounded;
}

bool TextContents::Render(const ContentContext& renderer,
Expand Down
5 changes: 5 additions & 0 deletions impeller/entity/contents/text_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class TextContents final : public Contents {
const Entity& entity,
RenderPass& pass) const override;

/// @brief Round font scales from arbitrary floating point values
/// (2.4223123) to one decimal place (2.4). This ensures that small
/// transform changes do not invalidate glyphs unecessarily.
static Scalar RoundFontScale(Scalar value);

private:
TextFrame frame_;
Scalar scale_ = 1.0;
Expand Down
7 changes: 7 additions & 0 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,13 @@ TEST_P(EntityTest, ColorFilterContentsWithLargeGeometry) {
ASSERT_TRUE(OpenPlaygroundHere(entity));
}

TEST_P(EntityTest, TextContentsCeilsGlyphScaleToDecimal) {
ASSERT_EQ(TextContents::RoundFontScale(0.4321111f), 0.4f);
ASSERT_EQ(TextContents::RoundFontScale(0.5321111f), 0.5f);
ASSERT_EQ(TextContents::RoundFontScale(2.1f), 2.1f);
ASSERT_EQ(TextContents::RoundFontScale(0.0f), 0.0f);
}

} // namespace testing
} // namespace impeller

Expand Down