[Impeller] clamp maximum glyph scale. (flutter/engine#52403)

Workaround for https://github.com/flutter/flutter/issues/136112

If the glyph scale is too large, say in the hundreds or thousands, then glyph itself will likely be too big to fit in the atlas. Instead of failing to render - clamp the scale (not the size, which is bounds * scale) to a much lower scaling parameter.
This commit is contained in:
Jonah Williams
2024-04-26 10:06:13 -07:00
committed by GitHub
parent 2015604d7a
commit 3cae3c9f7b
4 changed files with 28 additions and 1 deletions

View File

@@ -3192,6 +3192,24 @@ TEST_P(AiksTest, StrokedPathWithMoveToThenCloseDrawnCorrectly) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, CanRenderTextWithLargePerspectiveTransform) {
// Verifies that text scales are clamped to work around
// https://github.com/flutter/flutter/issues/136112 .
Canvas canvas;
Paint save_paint;
canvas.SaveLayer(save_paint);
canvas.Transform(Matrix(2000, 0, 0, 0, //
0, 2000, 0, 0, //
0, 0, -1, 9000, //
0, 0, -1, 7000 //
));
ASSERT_TRUE(RenderTextInCanvasSkia(GetContext(), canvas, "Hello world",
"Roboto-Regular.ttf"));
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
} // namespace testing
} // namespace impeller

View File

@@ -2566,6 +2566,7 @@ TEST_P(EntityTest, TextContentsCeilsGlyphScaleToDecimal) {
ASSERT_EQ(TextFrame::RoundScaledFontSize(0.5321111f, 12), 0.53f);
ASSERT_EQ(TextFrame::RoundScaledFontSize(2.1f, 12), 2.1f);
ASSERT_EQ(TextFrame::RoundScaledFontSize(0.0f, 12), 0.0f);
ASSERT_EQ(TextFrame::RoundScaledFontSize(100000000.0f, 12), 48.0f);
}
TEST_P(EntityTest, AdvancedBlendCoverageHintIsNotResetByEntityPass) {

View File

@@ -64,7 +64,12 @@ bool TextFrame::MaybeHasOverlapping() const {
// static
Scalar TextFrame::RoundScaledFontSize(Scalar scale, Scalar point_size) {
return std::round(scale * 100) / 100;
// An arbitrarily chosen maximum text scale to ensure that regardless of the
// CTM, a glyph will fit in the atlas. If we clamp significantly, this may
// reduce fidelity but is preferable to the alternative of failing to render.
constexpr Scalar kMaximumTextScale = 48;
Scalar result = std::round(scale * 100) / 100;
return std::clamp(result, 0.0f, kMaximumTextScale);
}
void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map,

View File

@@ -451,6 +451,9 @@ impeller_Play_AiksTest_CanRenderTextInSaveLayer_Vulkan.png
impeller_Play_AiksTest_CanRenderTextOutsideBoundaries_Metal.png
impeller_Play_AiksTest_CanRenderTextOutsideBoundaries_OpenGLES.png
impeller_Play_AiksTest_CanRenderTextOutsideBoundaries_Vulkan.png
impeller_Play_AiksTest_CanRenderTextWithLargePerspectiveTransform_Metal.png
impeller_Play_AiksTest_CanRenderTextWithLargePerspectiveTransform_OpenGLES.png
impeller_Play_AiksTest_CanRenderTextWithLargePerspectiveTransform_Vulkan.png
impeller_Play_AiksTest_CanRenderThickCurvedStrokes_Metal.png
impeller_Play_AiksTest_CanRenderThickCurvedStrokes_OpenGLES.png
impeller_Play_AiksTest_CanRenderThickCurvedStrokes_Vulkan.png