Skip to content

Commit f5fbfa8

Browse files
author
Jonah Williams
authored
[Impeller] round scale for glyph atlas cache to 2 decimal places. (flutter#43752)
Fixes flutter#130750
1 parent ed34ad0 commit f5fbfa8

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

impeller/entity/contents/text_contents.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,12 @@ bool TextContents::Render(const ContentContext& renderer,
176176
size_t vertex_offset = 0;
177177
for (const auto& run : frame_.GetRuns()) {
178178
const Font& font = run.GetFont();
179+
auto rounded_scale = TextFrame::RoundScaledFontSize(
180+
scale_, font.GetMetrics().point_size);
181+
179182
for (const auto& glyph_position : run.GetGlyphPositions()) {
180-
FontGlyphPair font_glyph_pair{font, glyph_position.glyph, scale_};
183+
FontGlyphPair font_glyph_pair{font, glyph_position.glyph,
184+
rounded_scale};
181185
auto maybe_atlas_glyph_bounds =
182186
atlas->FindFontGlyphBounds(font_glyph_pair);
183187
if (!maybe_atlas_glyph_bounds.has_value()) {

impeller/entity/entity_unittests.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,6 +2426,13 @@ TEST_P(EntityTest, ColorFilterContentsWithLargeGeometry) {
24262426
ASSERT_TRUE(OpenPlaygroundHere(entity));
24272427
}
24282428

2429+
TEST_P(EntityTest, TextContentsCeilsGlyphScaleToDecimal) {
2430+
ASSERT_EQ(TextFrame::RoundScaledFontSize(0.4321111f, 12), 0.43f);
2431+
ASSERT_EQ(TextFrame::RoundScaledFontSize(0.5321111f, 12), 0.53f);
2432+
ASSERT_EQ(TextFrame::RoundScaledFontSize(2.1f, 12), 2.1f);
2433+
ASSERT_EQ(TextFrame::RoundScaledFontSize(0.0f, 12), 0.0f);
2434+
}
2435+
24292436
} // namespace testing
24302437
} // namespace impeller
24312438

impeller/typographer/text_frame.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,27 @@ bool TextFrame::MaybeHasOverlapping() const {
7979
return false;
8080
}
8181

82+
// static
83+
Scalar TextFrame::RoundScaledFontSize(Scalar scale, Scalar point_size) {
84+
return std::round(scale * 100) / 100;
85+
}
86+
8287
void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphPair::Set& set,
8388
Scalar scale) const {
8489
for (const TextRun& run : GetRuns()) {
8590
const Font& font = run.GetFont();
91+
auto rounded_scale =
92+
RoundScaledFontSize(scale, font.GetMetrics().point_size);
8693
for (const TextRun::GlyphPosition& glyph_position :
8794
run.GetGlyphPositions()) {
88-
set.insert({font, glyph_position.glyph, scale});
95+
#if false
96+
// Glyph size error due to RoundScaledFontSize usage above.
97+
if (rounded_scale != scale) {
98+
auto delta = std::abs(rounded_scale - scale);
99+
FML_LOG(ERROR) << glyph_position.glyph.bounds.size * delta;
100+
}
101+
#endif
102+
set.insert({font, glyph_position.glyph, rounded_scale});
89103
}
90104
}
91105
}

impeller/typographer/text_frame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class TextFrame {
2424

2525
void CollectUniqueFontGlyphPairs(FontGlyphPair::Set& set, Scalar scale) const;
2626

27+
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size);
28+
2729
//----------------------------------------------------------------------------
2830
/// @brief The conservative bounding box for this text frame.
2931
///

0 commit comments

Comments
 (0)