From 63bc51106ccad2fa07646bfe76da9505da1fb32d Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Mon, 5 Jun 2017 12:08:21 -0700 Subject: [PATCH] Add command line option to supply --font-directory= Change-Id: If3036a32a82249c9c95d2c5722bdb5ab097c20a1 --- engine/src/flutter/src/font_collection.cc | 14 +++--- engine/src/flutter/src/font_collection.h | 3 +- engine/src/flutter/src/paragraph.cc | 11 ++--- engine/src/flutter/src/paragraph.h | 5 ++- engine/src/flutter/tests/txt/BUILD.gn | 3 ++ .../tests/txt/font_collection_unittests.cc | 8 +++- .../flutter/tests/txt/paragraph_unittests.cc | 6 ++- .../tests/txt/txt_run_all_unittests.cc | 17 ++++++++ engine/src/flutter/tests/txt/utils.cc | 43 +++++++++++++++++++ engine/src/flutter/tests/txt/utils.h | 31 +++++++++++++ engine/src/flutter/tests/util/FileUtils.cpp | 17 ++++---- engine/src/flutter/tests/util/FileUtils.h | 1 - 12 files changed, 133 insertions(+), 26 deletions(-) create mode 100644 engine/src/flutter/tests/txt/utils.cc create mode 100644 engine/src/flutter/tests/txt/utils.h diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc index dc0207b4e5..74596e102f 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_collection.cc @@ -21,6 +21,7 @@ #include "lib/ftl/logging.h" #include "lib/txt/src/font_skia.h" #include "third_party/skia/include/ports/SkFontMgr.h" +#include "third_party/skia/include/ports/SkFontMgr_directory.h" namespace txt { @@ -36,17 +37,21 @@ FontCollection::FontCollection() = default; FontCollection::~FontCollection() = default; std::shared_ptr -FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) { +FontCollection::GetMinikinFontCollectionForFamily(const std::string& family, + const std::string& dir) { // Get the Skia font manager. - auto skia_font_manager = SkFontMgr::RefDefault(); + auto skia_font_manager = dir.length() != 0 + ? SkFontMgr_New_Custom_Directory(dir.c_str()) + : SkFontMgr::RefDefault(); + FTL_DCHECK(skia_font_manager != nullptr); // Ask Skia to resolve a font style set for a font family name. - // FIXME(chinmaygarde): The name "Hevetica" is hardcoded because CoreText + // FIXME(chinmaygarde): The name "Sample Font" is hardcoded because CoreText // crashes when passed a null string. This seems to be a bug in Skia as // SkFontMgr explicitly says passing in nullptr gives the default font. auto font_style_set = skia_font_manager->matchFamily( - family.length() == 0 ? "Helvetica" : family.c_str()); + family.length() == 0 ? "Sample Font" : family.c_str()); FTL_DCHECK(font_style_set != nullptr); std::vector minikin_fonts; @@ -56,7 +61,6 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) { // Create the skia typeface auto skia_typeface = sk_ref_sp(font_style_set->createTypeface(i)); - if (skia_typeface == nullptr) { continue; } diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h index efca9e35c2..a85963a6bf 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_collection.h @@ -36,7 +36,8 @@ class FontCollection { ~FontCollection(); std::shared_ptr GetMinikinFontCollectionForFamily( - const std::string& family); + const std::string& family, + const std::string& dir = ""); private: // TODO(chinmaygarde): Caches go here. diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 4fe7d785e7..dad0c69127 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -113,9 +113,9 @@ void Paragraph::SetText(std::vector text, StyledRuns runs) { breaker_.setText(); } -void Paragraph::AddRunsToLineBreaker() { +void Paragraph::AddRunsToLineBreaker(const std::string& rootdir) { auto collection = FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily(""); + .GetMinikinFontCollectionForFamily("", rootdir); minikin::FontStyle font; minikin::MinikinPaint paint; for (size_t i = 0; i < runs_.size(); ++i) { @@ -125,9 +125,10 @@ void Paragraph::AddRunsToLineBreaker() { } } -void Paragraph::Layout(const ParagraphConstraints& constraints) { +void Paragraph::Layout(const ParagraphConstraints& constraints, + const std::string& rootdir) { breaker_.setLineWidths(0.0f, 0, constraints.width()); - AddRunsToLineBreaker(); + AddRunsToLineBreaker(rootdir); size_t breaks_count = breaker_.computeBreaks(); const int* breaks = breaker_.getBreaks(); @@ -140,7 +141,7 @@ void Paragraph::Layout(const ParagraphConstraints& constraints) { SkTextBlobBuilder builder; auto collection = FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily(""); + .GetMinikinFontCollectionForFamily("", rootdir); minikin::Layout layout; SkScalar x = 0.0f; SkScalar y = 0.0f; diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 4fd5d5aff2..eee7f43583 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -36,7 +36,8 @@ class Paragraph { ~Paragraph(); - void Layout(const ParagraphConstraints& constraints); + void Layout(const ParagraphConstraints& constraints, + const std::string& rootdir = ""); void Paint(SkCanvas* canvas, double x, double y); @@ -50,7 +51,7 @@ class Paragraph { void SetText(std::vector text, StyledRuns runs); - void AddRunsToLineBreaker(); + void AddRunsToLineBreaker(const std::string& rootdir = ""); FTL_DISALLOW_COPY_AND_ASSIGN(Paragraph); }; diff --git a/engine/src/flutter/tests/txt/BUILD.gn b/engine/src/flutter/tests/txt/BUILD.gn index 93e97a1c86..3fb1019628 100644 --- a/engine/src/flutter/tests/txt/BUILD.gn +++ b/engine/src/flutter/tests/txt/BUILD.gn @@ -25,7 +25,10 @@ executable("txt") { "font_collection_unittests.cc", "paragraph_unittests.cc", "render_test.cc", + "render_test.h", "txt_run_all_unittests.cc", + "utils.cc", + "utils.h", ] deps = [ diff --git a/engine/src/flutter/tests/txt/font_collection_unittests.cc b/engine/src/flutter/tests/txt/font_collection_unittests.cc index 9eaf955daa..1be73be10f 100644 --- a/engine/src/flutter/tests/txt/font_collection_unittests.cc +++ b/engine/src/flutter/tests/txt/font_collection_unittests.cc @@ -16,10 +16,14 @@ #include "font_collection.h" #include "gtest/gtest.h" +#include "lib/ftl/command_line.h" +#include "lib/ftl/logging.h" +#include "lib/txt/tests/txt/utils.h" TEST(FontCollection, HasDefaultRegistrations) { - auto collection = txt::FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily(""); + auto collection = + txt::FontCollection::GetDefaultFontCollection() + .GetMinikinFontCollectionForFamily("", txt::GetFontDir()); ASSERT_NE(collection.get(), nullptr); } diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 9ea2d3d306..723b5d828f 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "lib/ftl/logging.h" +#include "lib/txt/tests/txt/utils.h" #include "paragraph.h" #include "paragraph_builder.h" #include "render_test.h" @@ -38,8 +40,8 @@ TEST_F(RenderTest, SimpleParagraph) { builder.Pop(); auto paragraph = builder.Build(); - - paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}); + paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}, + txt::GetFontDir()); paragraph->Paint(GetCanvas(), 10.0, 10.0); diff --git a/engine/src/flutter/tests/txt/txt_run_all_unittests.cc b/engine/src/flutter/tests/txt/txt_run_all_unittests.cc index a105847fea..d5c62a171f 100644 --- a/engine/src/flutter/tests/txt/txt_run_all_unittests.cc +++ b/engine/src/flutter/tests/txt/txt_run_all_unittests.cc @@ -16,9 +16,26 @@ #include "flutter/fml/icu_util.h" #include "gtest/gtest.h" +#include "lib/ftl/command_line.h" +#include "lib/ftl/logging.h" +#include "lib/txt/tests/txt/utils.h" #include "third_party/skia/include/core/SkGraphics.h" +#include + int main(int argc, char** argv) { + ftl::CommandLine cmd = ftl::CommandLineFromArgcArgv(argc, argv); + txt::SetCommandLine(cmd); + std::string dir = txt::GetCommandLineForProcess().GetOptionValueWithDefault( + "font-directory", ""); + txt::SetFontDir(dir); + if (txt::GetFontDir().length() <= 0) { + FTL_LOG(ERROR) << "Font directory must be specified with " + "--font-directoy=\"\" to run this test."; + return EXIT_FAILURE; + } + FTL_DCHECK(txt::GetFontDir().length() > 0); + fml::icu::InitializeICU(); SkGraphics::Init(); testing::InitGoogleTest(&argc, argv); diff --git a/engine/src/flutter/tests/txt/utils.cc b/engine/src/flutter/tests/txt/utils.cc new file mode 100644 index 0000000000..3f853b8a92 --- /dev/null +++ b/engine/src/flutter/tests/txt/utils.cc @@ -0,0 +1,43 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "lib/ftl/command_line.h" +#include "lib/txt/tests/txt/utils.h" + +namespace txt { + +static std::string gFontdir; +static ftl::CommandLine gCommandLine; + +const std::string GetFontDir() { + return gFontdir; +} + +void SetFontDir(std::string& dir) { + gFontdir = dir; +} + +const ftl::CommandLine& GetCommandLineForProcess() { + return gCommandLine; +} + +void SetCommandLine(ftl::CommandLine cmd) { + gCommandLine = std::move(cmd); +} + +} // namespace txt diff --git a/engine/src/flutter/tests/txt/utils.h b/engine/src/flutter/tests/txt/utils.h new file mode 100644 index 0000000000..ed42b82073 --- /dev/null +++ b/engine/src/flutter/tests/txt/utils.h @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "lib/ftl/command_line.h" + +namespace txt { + +const std::string GetFontDir(); + +void SetFontDir(std::string& dir); + +const ftl::CommandLine& GetCommandLineForProcess(); + +void SetCommandLine(ftl::CommandLine cmd); + +} // namespace txt diff --git a/engine/src/flutter/tests/util/FileUtils.cpp b/engine/src/flutter/tests/util/FileUtils.cpp index 92cf9191bc..07f65342ab 100644 --- a/engine/src/flutter/tests/util/FileUtils.cpp +++ b/engine/src/flutter/tests/util/FileUtils.cpp @@ -23,13 +23,14 @@ #include std::vector readWholeFile(const std::string& filePath) { - FILE* fp = fopen(filePath.c_str(), "r"); - LOG_ALWAYS_FATAL_IF(fp == nullptr); - struct stat st; - LOG_ALWAYS_FATAL_IF(fstat(fileno(fp), &st) != 0); + FILE* fp = fopen(filePath.c_str(), "r"); + LOG_ALWAYS_FATAL_IF(fp == nullptr); + struct stat st; + LOG_ALWAYS_FATAL_IF(fstat(fileno(fp), &st) != 0); - std::vector result(st.st_size); - LOG_ALWAYS_FATAL_IF(fread(result.data(), 1, st.st_size, fp) != static_cast(st.st_size)); - fclose(fp); - return result; + std::vector result(st.st_size); + LOG_ALWAYS_FATAL_IF(fread(result.data(), 1, st.st_size, fp) != + static_cast(st.st_size)); + fclose(fp); + return result; } diff --git a/engine/src/flutter/tests/util/FileUtils.h b/engine/src/flutter/tests/util/FileUtils.h index 1e66d1b701..e9c5ef1c34 100644 --- a/engine/src/flutter/tests/util/FileUtils.h +++ b/engine/src/flutter/tests/util/FileUtils.h @@ -15,4 +15,3 @@ */ std::vector readWholeFile(const std::string& filePath); -