[Impeller] fix emojis with non-solid color sources. (flutter/engine#53229)

Fixes https://github.com/flutter/flutter/issues/142974

If a text is drawn with a color source but lacks a path, just draw the original text. This ensures emojis render like Skia.
This commit is contained in:
Jonah Williams
2024-06-05 16:24:14 -07:00
committed by GitHub
parent b183150d74
commit 8d062af40c
3 changed files with 89 additions and 3 deletions

View File

@@ -122,7 +122,14 @@ source_set("txt") {
if (enable_unittests) {
test_fixtures("txt_fixtures") {
fixtures = [ "third_party/fonts/Roboto-Regular.ttf" ]
fixtures = [
"third_party/fonts/Roboto-Regular.ttf",
"third_party/fonts/NotoColorEmoji.ttf",
]
if (host_os == "mac") {
fixtures += [ "/System/Library/Fonts/Apple Color Emoji.ttc" ]
}
}
executable("txt_benchmarks") {
@@ -176,8 +183,7 @@ if (enable_unittests) {
# This is needed for //flutter/third_party/googletest for linking zircon
# symbols.
if (is_fuchsia) {
libs =
[ "${fuchsia_arch_root}/sysroot/lib/libzircon.so" ]
libs = [ "${fuchsia_arch_root}/sysroot/lib/libzircon.so" ]
}
}
}

View File

@@ -87,6 +87,16 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter {
if (impeller_enabled_) {
if (ShouldRenderAsPath(dl_paints_[paint_id])) {
auto path = skia::textlayout::Paragraph::GetPath(blob.get());
// If there is no path, this is an emoji and should be drawn as is,
// ignoring the color source.
if (path.isEmpty()) {
builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(
blob, dl_paints_[paint_id].getColor()),
x, y, dl_paints_[paint_id]);
return;
}
auto transformed = path.makeTransform(SkMatrix::Translate(
x + blob->bounds().left(), y + blob->bounds().top()));
builder_->DrawPath(transformed, dl_paints_[paint_id]);

View File

@@ -13,10 +13,27 @@
#include "runtime/test_font_data.h"
#include "skia/paragraph_builder_skia.h"
#include "testing/canvas_test.h"
#include "testing/testing.h"
#include "txt/platform.h"
namespace flutter {
namespace testing {
static const std::string kEmojiFontFile =
#if FML_OS_MACOSX
"Apple Color Emoji.ttc";
#else
"NotoColorEmoji.ttf";
#endif
static const std::string kEmojiFontName =
#if FML_OS_MACOSX
"Apple Color Emoji";
#else
"Noto Color Emoji";
#endif
//------------------------------------------------------------------------------
/// @brief A custom |DlOpReceiver| that records some |DlOps| it receives.
class DlOpRecorder final : public virtual DlOpReceiver,
@@ -85,6 +102,15 @@ class PainterTestBase : public CanvasTestBase<T> {
return t_style;
}
txt::TextStyle makeEmoji() {
auto t_style = txt::TextStyle();
t_style.color = SK_ColorBLACK; // default
t_style.font_weight = txt::FontWeight::w400; // normal
t_style.font_size = 14; // default
t_style.font_families.push_back(kEmojiFontName);
return t_style;
}
txt::TextStyle makeStyle() {
auto t_style = txt::TextStyle();
t_style.color = SK_ColorBLACK; // default
@@ -94,6 +120,20 @@ class PainterTestBase : public CanvasTestBase<T> {
return t_style;
}
sk_sp<DisplayList> drawText(txt::TextStyle style, std::u16string text) const {
auto pb_skia = makeParagraphBuilder();
pb_skia.PushStyle(style);
pb_skia.AddText(text);
pb_skia.Pop();
auto builder = DisplayListBuilder();
auto paragraph = pb_skia.Build();
paragraph->Layout(10000);
paragraph->Paint(&builder, 0, 0);
return builder.Build();
}
sk_sp<DisplayList> draw(txt::TextStyle style) const {
auto pb_skia = makeParagraphBuilder();
pb_skia.PushStyle(style);
@@ -115,6 +155,15 @@ class PainterTestBase : public CanvasTestBase<T> {
for (auto& font : GetTestFontData()) {
font_provider->RegisterTypeface(font);
}
// Load emoji font.
{
auto c_font_fixture = std::string(kEmojiFontFile);
auto mapping =
flutter::testing::OpenFixtureAsSkData(c_font_fixture.c_str());
auto typeface = txt::GetDefaultFontManager()->makeFromData(mapping);
font_provider->RegisterTypeface(typeface);
}
auto manager = sk_make_sp<txt::AssetFontManager>(std::move(font_provider));
f_collection->SetAssetFontManager(manager);
return f_collection;
@@ -230,6 +279,27 @@ TEST_F(PainterTest, DrawTextWithGradientImpeller) {
EXPECT_EQ(recorder.pathCount(), 1);
}
TEST_F(PainterTest, DrawEmojiTextWithGradientImpeller) {
PretendImpellerIsEnabled(true);
auto style = makeEmoji();
// how do you like my shtyle?
DlPaint foreground;
std::vector<DlColor> colors = {DlColor::kRed(), DlColor::kCyan()};
std::vector<float> stops = {0.0, 1.0};
foreground.setColorSource(DlColorSource::MakeLinear(
SkPoint::Make(0, 0), SkPoint::Make(100, 100), 2, colors.data(),
stops.data(), DlTileMode::kClamp));
style.foreground = foreground;
auto recorder = DlOpRecorder();
drawText(style, u"😀 😃 😄 😁 😆 😅 😂 🤣 🥲 😊")->Dispatch(recorder);
EXPECT_EQ(recorder.textFrameCount(), 1);
EXPECT_EQ(recorder.blobCount(), 0);
EXPECT_EQ(recorder.pathCount(), 0);
}
TEST_F(PainterTest, DrawTextBlobNoImpeller) {
PretendImpellerIsEnabled(false);