From 6b147dc9c37d1b8005d3c682acf3331e251ba04e Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Sat, 22 Jul 2023 14:28:29 -0700 Subject: [PATCH] [Impeller] Fix text glyph bounds on Android. (flutter/engine#43919) Resolves https://github.com/flutter/flutter/issues/128624. It turns out that `SkFont::getBounds()` snaps results to integers on Android, but not iOS. By scaling the font up and scaling the resulting per-glyph bounds back down, we can ensure that the results are always precise enough. I also found errors with our usage of the computed bounds, but those were comparatively minor fixes. --- .../backends/skia/text_frame_skia.cc | 17 ++++++++++++++--- .../backends/skia/text_render_context_skia.cc | 4 ++-- .../typographer/typographer_unittests.cc | 4 +--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc index cac02a8096..d458499c9d 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc @@ -37,6 +37,8 @@ static Rect ToRect(const SkRect& rect) { return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); } +static constexpr Scalar kScaleSize = 100000.0f; + TextFrame TextFrameFromTextBlob(const sk_sp& blob) { if (!blob) { return {}; @@ -64,7 +66,14 @@ TextFrame TextFrameFromTextBlob(const sk_sp& blob) { case SkTextBlobRunIterator::kFull_Positioning: { std::vector glyph_bounds; glyph_bounds.resize(glyph_count); - run.font().getBounds(glyphs, glyph_count, glyph_bounds.data(), nullptr); + SkFont font = run.font(); + auto font_size = font.getSize(); + // For some platforms (including Android), `SkFont::getBounds()` snaps + // the computed bounds to integers. And so we scale up the font size + // prior to fetching the bounds to ensure that the returned bounds are + // always precise enough. + font.setSize(kScaleSize); + font.getBounds(glyphs, glyph_count, glyph_bounds.data(), nullptr); for (auto i = 0u; i < glyph_count; i++) { // kFull_Positioning has two scalars per glyph. @@ -74,8 +83,10 @@ TextFrame TextFrameFromTextBlob(const sk_sp& blob) { ? Glyph::Type::kBitmap : Glyph::Type::kPath; - text_run.AddGlyph(Glyph{glyphs[i], type, ToRect(glyph_bounds[i])}, - Point{point->x(), point->y()}); + text_run.AddGlyph( + Glyph{glyphs[i], type, + ToRect(glyph_bounds[i]).Scale(font_size / kScaleSize)}, + Point{point->x(), point->y()}); } break; } diff --git a/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc index 8553ce87a7..1dca003c66 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc @@ -56,7 +56,7 @@ static size_t PairsFitInAtlasOfSize( for (auto it = pairs.begin(); it != pairs.end(); ++i, ++it) { const auto& pair = *it; - const auto glyph_size = ISize::Ceil((pair.glyph.bounds * pair.scale).size); + const auto glyph_size = ISize::Ceil(pair.glyph.bounds.size * pair.scale); IPoint16 location_in_atlas; if (!rect_packer->addRect(glyph_size.width + kPadding, // glyph_size.height + kPadding, // @@ -93,7 +93,7 @@ static bool CanAppendToExistingAtlas( for (size_t i = 0; i < extra_pairs.size(); i++) { const FontGlyphPair& pair = extra_pairs[i]; - const auto glyph_size = ISize::Ceil((pair.glyph.bounds * pair.scale).size); + const auto glyph_size = ISize::Ceil(pair.glyph.bounds.size * pair.scale); IPoint16 location_in_atlas; if (!rect_packer->addRect(glyph_size.width + kPadding, // glyph_size.height + kPadding, // diff --git a/engine/src/flutter/impeller/typographer/typographer_unittests.cc b/engine/src/flutter/impeller/typographer/typographer_unittests.cc index e800ea258b..b2da0e6b80 100644 --- a/engine/src/flutter/impeller/typographer/typographer_unittests.cc +++ b/engine/src/flutter/impeller/typographer/typographer_unittests.cc @@ -311,9 +311,7 @@ TEST_P(TypographerTest, MaybeHasOverlapping) { auto frame_2 = TextFrameFromTextBlob(SkTextBlob::MakeFromString("123456789", sk_font)); - // Characters probably have overlap due to low fidelity text metrics, but this - // could be fixed. - ASSERT_TRUE(frame_2.MaybeHasOverlapping()); + ASSERT_FALSE(frame_2.MaybeHasOverlapping()); } TEST_P(TypographerTest, RectanglePackerAddsNonoverlapingRectangles) {