diff --git a/display_list/BUILD.gn b/display_list/BUILD.gn index 1ec13109d0d83..c1905573cdb28 100644 --- a/display_list/BUILD.gn +++ b/display_list/BUILD.gn @@ -127,6 +127,7 @@ if (enable_unittests) { ":display_list_fixtures", "//flutter/display_list/testing:display_list_testing", "//flutter/testing", + "//flutter/third_party/txt:txt", ] if (!defined(defines)) { @@ -222,6 +223,7 @@ source_set("display_list_benchmarks_source") { "//flutter/fml", "//flutter/testing:skia", "//flutter/testing:testing_lib", + "//flutter/third_party/txt:txt", "//third_party/dart/runtime:libdart_jit", # for tracing "//third_party/skia", ] diff --git a/display_list/benchmarking/dl_benchmarks.cc b/display_list/benchmarking/dl_benchmarks.cc index 2a03b60c7a8e9..7e85ddd09db03 100644 --- a/display_list/benchmarking/dl_benchmarks.cc +++ b/display_list/benchmarking/dl_benchmarks.cc @@ -15,6 +15,7 @@ #include "third_party/skia/include/gpu/GrDirectContext.h" #include "third_party/skia/include/gpu/GrRecordingContext.h" #include "third_party/skia/include/gpu/GrTypes.h" +#include "third_party/txt/src/txt/platform.h" namespace flutter { namespace testing { @@ -1203,7 +1204,7 @@ void BM_DrawTextBlob(benchmark::State& state, for (size_t i = 0; i < draw_calls; i++) { character[0] = 'A' + (i % 26); - auto blob = SkTextBlob::MakeFromString(character, SkFont()); + auto blob = SkTextBlob::MakeFromString(character, txt::DefaultFont()); builder.DrawTextBlob(blob, 50.0f, 50.0f, paint); } diff --git a/display_list/benchmarking/dl_complexity_unittests.cc b/display_list/benchmarking/dl_complexity_unittests.cc index df4fc68497336..63c24d36da5cc 100644 --- a/display_list/benchmarking/dl_complexity_unittests.cc +++ b/display_list/benchmarking/dl_complexity_unittests.cc @@ -14,6 +14,7 @@ #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkImage.h" +#include "third_party/txt/src/txt/platform.h" namespace flutter { namespace testing { @@ -306,7 +307,7 @@ TEST(DisplayListComplexity, DrawVertices) { TEST(DisplayListComplexity, DrawTextBlob) { auto text_blob = SkTextBlob::MakeFromString( - "The quick brown fox jumps over the lazy dog.", SkFont()); + "The quick brown fox jumps over the lazy dog.", txt::DefaultFont()); DisplayListBuilder builder; builder.DrawTextBlob(text_blob, 0.0f, 0.0f, DlPaint()); diff --git a/display_list/testing/BUILD.gn b/display_list/testing/BUILD.gn index 1839219b1e8e3..aae80f7c3c7f3 100644 --- a/display_list/testing/BUILD.gn +++ b/display_list/testing/BUILD.gn @@ -16,6 +16,7 @@ source_set("display_list_testing") { deps = [ "//flutter/testing:testing_lib", + "//flutter/third_party/txt:txt", "//third_party/skia", ] diff --git a/display_list/testing/dl_test_snippets.h b/display_list/testing/dl_test_snippets.h index 64f54362cae7d..a85881f18eb00 100644 --- a/display_list/testing/dl_test_snippets.h +++ b/display_list/testing/dl_test_snippets.h @@ -13,6 +13,7 @@ #include "third_party/skia/include/effects/SkDashPathEffect.h" #include "third_party/skia/include/effects/SkGradientShader.h" #include "third_party/skia/include/effects/SkImageFilters.h" +#include "third_party/txt/src/txt/platform.h" namespace flutter { namespace testing { @@ -223,8 +224,8 @@ static sk_sp TestDisplayList2 = MakeTestDisplayList(25, 25, SK_ColorBLUE); static sk_sp MakeTextBlob(std::string string) { - return SkTextBlob::MakeFromText(string.c_str(), string.size(), SkFont(), - SkTextEncoding::kUTF8); + return SkTextBlob::MakeFromText(string.c_str(), string.size(), + txt::DefaultFont(), SkTextEncoding::kUTF8); } static sk_sp TestBlob1 = MakeTextBlob("TestBlob1"); static sk_sp TestBlob2 = MakeTextBlob("TestBlob2"); diff --git a/third_party/txt/BUILD.gn b/third_party/txt/BUILD.gn index da4d0615d7b9a..66aff09556071 100644 --- a/third_party/txt/BUILD.gn +++ b/third_party/txt/BUILD.gn @@ -62,6 +62,7 @@ source_set("txt") { "src/txt/placeholder_run.cc", "src/txt/placeholder_run.h", "src/txt/platform.h", + "src/txt/platform_common.cc", "src/txt/run_metrics.h", "src/txt/test_font_manager.cc", "src/txt/test_font_manager.h", diff --git a/third_party/txt/src/txt/platform.cc b/third_party/txt/src/txt/platform.cc index f1860efd7e9cf..bc75184dbe0ab 100644 --- a/third_party/txt/src/txt/platform.cc +++ b/third_party/txt/src/txt/platform.cc @@ -4,6 +4,8 @@ #include "txt/platform.h" +#include "fml/logging.h" + namespace txt { std::vector GetDefaultFontFamilies() { @@ -11,6 +13,8 @@ std::vector GetDefaultFontFamilies() { } sk_sp GetDefaultFontManager(uint32_t font_initialization_data) { + // TODO(b/305780908) Replace this with a singleton that depends on which + // platform we are on and which SkFontMgr was compiled in. return SkFontMgr::RefDefault(); } diff --git a/third_party/txt/src/txt/platform.h b/third_party/txt/src/txt/platform.h index 7c29e1900121d..55a9812e4a8bb 100644 --- a/third_party/txt/src/txt/platform.h +++ b/third_party/txt/src/txt/platform.h @@ -9,14 +9,21 @@ #include #include "flutter/fml/macros.h" +#include "third_party/skia/include/core/SkFont.h" #include "third_party/skia/include/core/SkFontMgr.h" +#include "third_party/skia/include/core/SkRefCnt.h" namespace txt { std::vector GetDefaultFontFamilies(); +// Returns the platform specific SkFontMgr, which is a singleton. sk_sp GetDefaultFontManager(uint32_t font_initialization_data); +// Returns a font using a default typeface returned by a platform-specific +// SkFontMgr. It may be different on different platforms. +SkFont DefaultFont(); + } // namespace txt #endif // TXT_PLATFORM_H_ diff --git a/third_party/txt/src/txt/platform_common.cc b/third_party/txt/src/txt/platform_common.cc new file mode 100644 index 0000000000000..8aa39d4bd64d9 --- /dev/null +++ b/third_party/txt/src/txt/platform_common.cc @@ -0,0 +1,41 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "txt/platform.h" + +#include "fml/logging.h" +#include "third_party/skia/include/core/SkFont.h" +#include "third_party/skia/include/core/SkFontMgr.h" +#include "third_party/skia/include/core/SkFontStyle.h" +#include "third_party/skia/include/core/SkRefCnt.h" +#include "third_party/skia/include/core/SkTypeface.h" + +namespace txt { + +static sk_sp MakeTypefaceFromName(const char* name, + SkFontStyle style) { + sk_sp fm = GetDefaultFontManager(0); + FML_CHECK(fm); + sk_sp face = fm->legacyMakeTypeface(name, style); + return face; +} + +static sk_sp DefaultTypeface() { + sk_sp face = MakeTypefaceFromName(nullptr, SkFontStyle()); + if (face) { + return face; + } + // Due to how SkTypeface::MakeDefault() used to work, many callers of this + // depend on the returned SkTypeface being non-null. + // TODO(kjlubick) replace this with SkTypeface::MakeEmpty() + face = SkTypeface::MakeDefault(); + FML_CHECK(face); + return face; +} + +SkFont DefaultFont() { + return SkFont(DefaultTypeface()); +} + +} // namespace txt