Removes assumption that basis scalar and rounded_scalar match (#165166)

fixes https://github.com/flutter/flutter/issues/165130

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
gaaclarke
2025-03-17 09:58:06 -07:00
committed by GitHub
parent 1ce0967351
commit 71a5b734c7
3 changed files with 59 additions and 4 deletions

View File

@@ -180,6 +180,35 @@ TEST_P(AiksTest, ScaledK) {
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_P(AiksTest, CanRenderTextFrameWithScalingOverflow) {
Scalar scale = 60.0;
Scalar offsetx = -500.0;
Scalar offsety = 700.0;
auto callback = [&]() -> sk_sp<DisplayList> {
if (AiksTest::ImGuiBegin("Controls", nullptr,
ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::SliderFloat("scale", &scale, 1.f, 300.f);
ImGui::SliderFloat("offsetx", &offsetx, -600.f, 100.f);
ImGui::SliderFloat("offsety", &offsety, 600.f, 2048.f);
ImGui::End();
}
DisplayListBuilder builder;
builder.Scale(GetContentScale().x, GetContentScale().y);
DlPaint paint;
paint.setColor(DlColor::ARGB(1, 0.1, 0.1, 0.1));
builder.DrawPaint(paint);
builder.Scale(scale, scale);
RenderTextInCanvasSkia(
GetContext(), builder, "test", "Roboto-Regular.ttf",
TextRenderOptions{
.position = DlPoint(offsetx / scale, offsety / scale),
});
return builder.Build();
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(AiksTest, CanRenderTextFrameWithFractionScaling) {
Scalar fine_scale = 0.f;
bool is_subpixel = false;

View File

@@ -77,6 +77,22 @@ void TextContents::SetTextProperties(Color color,
}
}
namespace {
Scalar AttractToOne(Scalar x) {
// Epsilon was decided by looking at the floating point inaccuracies in
// the ScaledK test.
const Scalar epsilon = 0.005f;
if (std::abs(x - 1.f) < epsilon) {
return 1.f;
}
if (std::abs(x + 1.f) < epsilon) {
return -1.f;
}
return x;
}
} // namespace
void TextContents::ComputeVertexData(
VS::PerVertexData* vtx_contents,
const std::shared_ptr<TextFrame>& frame,
@@ -165,8 +181,9 @@ void TextContents::ComputeVertexData(
atlas_glyph_bounds = maybe_atlas_glyph_bounds.value().atlas_bounds;
}
Rect scaled_bounds =
glyph_bounds.Scale(static_cast<Scalar>(rounded_scale.Invert()));
Scalar inverted_rounded_scale =
static_cast<Scalar>(rounded_scale.Invert());
Rect scaled_bounds = glyph_bounds.Scale(inverted_rounded_scale);
// For each glyph, we compute two rectangles. One for the vertex
// positions and one for the texture coordinates (UVs). The atlas
// glyph bounds are used to compute UVs in cases where the
@@ -176,8 +193,14 @@ void TextContents::ComputeVertexData(
Point uv_size = SizeToPoint(atlas_glyph_bounds.GetSize()) / atlas_size;
Matrix unscaled_basis =
Matrix::MakeScale({basis_transform.m[0] > 0 ? 1.f : -1.f,
basis_transform.m[5] > 0 ? 1.f : -1.f, 1.f});
basis_transform * Matrix::MakeScale({inverted_rounded_scale,
inverted_rounded_scale, 1});
// In typical scales < 48x these values should be -1 or 1. We round to
// those to avoid inaccuracies.
unscaled_basis.m[0] = AttractToOne(unscaled_basis.m[0]);
unscaled_basis.m[5] = AttractToOne(unscaled_basis.m[5]);
Point unrounded_glyph_position =
// This is for RTL text.
unscaled_basis * glyph_bounds.GetLeftTop() +

View File

@@ -490,6 +490,9 @@ impeller_Play_AiksTest_CanRenderTextFrameWithHalfScaling_Vulkan.png
impeller_Play_AiksTest_CanRenderTextFrameWithInvertedTransform_Metal.png
impeller_Play_AiksTest_CanRenderTextFrameWithInvertedTransform_OpenGLES.png
impeller_Play_AiksTest_CanRenderTextFrameWithInvertedTransform_Vulkan.png
impeller_Play_AiksTest_CanRenderTextFrameWithScalingOverflow_Metal.png
impeller_Play_AiksTest_CanRenderTextFrameWithScalingOverflow_OpenGLES.png
impeller_Play_AiksTest_CanRenderTextFrameWithScalingOverflow_Vulkan.png
impeller_Play_AiksTest_CanRenderTextFrame_Metal.png
impeller_Play_AiksTest_CanRenderTextFrame_OpenGLES.png
impeller_Play_AiksTest_CanRenderTextFrame_Vulkan.png