From 97cf68d6c87d50f8e05d63214d78b0d0ac002890 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 14 Jun 2018 16:13:56 -0700 Subject: [PATCH] Lazily load fonts that are embedded within the application (flutter/engine#5533) Previously the engine would load all embedded fonts listed in the app's font manifest during startup. This change creates a Skia font manager that is backed by the engine's AssetManager and can load embedded font assets on demand. --- engine/src/flutter/lib/ui/BUILD.gn | 2 + .../ui/text/asset_manager_font_provider.cc | 124 ++++++++++++++++++ .../lib/ui/text/asset_manager_font_provider.h | 79 +++++++++++ .../flutter/lib/ui/text/font_collection.cc | 36 +++-- .../src/flutter/lib/ui/text/font_collection.h | 2 +- engine/src/flutter/shell/common/engine.cc | 2 +- engine/src/flutter/third_party/txt/BUILD.gn | 28 ++-- .../txt/benchmarks/paint_record_benchmarks.cc | 2 +- .../txt/benchmarks/paragraph_benchmarks.cc | 2 +- .../paragraph_builder_benchmarks.cc | 2 +- .../txt/benchmarks/styled_runs_benchmarks.cc | 2 +- .../txt/benchmarks/txt_run_all_benchmarks.cc | 2 +- .../third_party/txt/benchmarks/utils.cc | 52 -------- .../txt/src/txt/asset_data_provider.cc | 73 ----------- .../txt/src/txt/asset_data_provider.h | 54 -------- .../txt/src/txt/asset_font_manager.cc | 18 ++- .../txt/src/txt/asset_font_manager.h | 6 +- .../txt/src/txt/asset_font_style_set.cc | 48 ------- .../txt/src/txt/asset_font_style_set.h | 44 ------- .../src/txt/directory_asset_data_provider.cc | 75 ----------- .../src/txt/directory_asset_data_provider.h | 25 ---- .../txt/font_asset_provider.h} | 34 ++--- .../txt/src/txt/test_font_manager.cc | 4 +- .../txt/src/txt/test_font_manager.h | 4 +- .../src/txt/typeface_font_asset_provider.cc | 118 +++++++++++++++++ .../src/txt/typeface_font_asset_provider.h | 84 ++++++++++++ .../txt/tests/font_collection_unittests.cc | 2 +- .../txt/tests/paragraph_unittests.cc | 2 +- .../third_party/txt/tests/render_test.cc | 8 +- .../txt/tests/txt_run_all_unittests.cc | 2 +- .../third_party/txt/tests/txt_test_utils.cc | 119 +++++++++++++++++ .../utils.h => tests/txt_test_utils.h} | 4 +- .../src/flutter/third_party/txt/tests/utils.h | 31 ----- 33 files changed, 603 insertions(+), 487 deletions(-) create mode 100644 engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc create mode 100644 engine/src/flutter/lib/ui/text/asset_manager_font_provider.h delete mode 100644 engine/src/flutter/third_party/txt/benchmarks/utils.cc delete mode 100644 engine/src/flutter/third_party/txt/src/txt/asset_data_provider.cc delete mode 100644 engine/src/flutter/third_party/txt/src/txt/asset_data_provider.h delete mode 100644 engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.cc delete mode 100644 engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.h delete mode 100644 engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.cc delete mode 100644 engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.h rename engine/src/flutter/third_party/txt/{tests/utils.cc => src/txt/font_asset_provider.h} (57%) create mode 100644 engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc create mode 100644 engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h create mode 100644 engine/src/flutter/third_party/txt/tests/txt_test_utils.cc rename engine/src/flutter/third_party/txt/{benchmarks/utils.h => tests/txt_test_utils.h} (95%) delete mode 100644 engine/src/flutter/third_party/txt/tests/utils.h diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index b90a8514a8..c8f7e1fb21 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -58,6 +58,8 @@ source_set("ui") { "semantics/semantics_update.h", "semantics/semantics_update_builder.cc", "semantics/semantics_update_builder.h", + "text/asset_manager_font_provider.cc", + "text/asset_manager_font_provider.h", "text/font_collection.cc", "text/font_collection.h", "text/paragraph.cc", diff --git a/engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc new file mode 100644 index 0000000000..68cb4ac3a9 --- /dev/null +++ b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc @@ -0,0 +1,124 @@ +// Copyright 2018 The Chromium 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 "flutter/lib/ui/text/asset_manager_font_provider.h" + +#include "lib/fxl/logging.h" +#include "third_party/skia/include/core/SkData.h" +#include "third_party/skia/include/core/SkStream.h" +#include "third_party/skia/include/core/SkString.h" +#include "third_party/skia/include/core/SkTypeface.h" + +namespace blink { + +namespace { + +void VectorReleaseProc(const void* ptr, void* context) { + delete reinterpret_cast*>(context); +} + +} // anonymous namespace + +AssetManagerFontProvider::AssetManagerFontProvider( + fml::RefPtr asset_manager) + : asset_manager_(asset_manager) {} + +AssetManagerFontProvider::~AssetManagerFontProvider() = default; + +// |FontAssetProvider| +size_t AssetManagerFontProvider::GetFamilyCount() const { + return family_names_.size(); +} + +// |FontAssetProvider| +std::string AssetManagerFontProvider::GetFamilyName(int index) const { + FXL_DCHECK(index >= 0 && static_cast(index) < family_names_.size()); + return family_names_[index]; +} + +// |FontAssetProvider| +SkFontStyleSet* AssetManagerFontProvider::MatchFamily( + const std::string& family_name) { + auto found = registered_families_.find(family_name); + if (found == registered_families_.end()) { + return nullptr; + } + return SkRef(&found->second); +} + +void AssetManagerFontProvider::RegisterAsset(std::string family_name, + std::string asset) { + auto family_it = registered_families_.find(family_name); + + if (family_it == registered_families_.end()) { + family_names_.push_back(family_name); + family_it = registered_families_ + .emplace(std::piecewise_construct, + std::forward_as_tuple(family_name), + std::forward_as_tuple(asset_manager_)) + .first; + } + + family_it->second.registerAsset(asset); +} + +AssetManagerFontStyleSet::AssetManagerFontStyleSet( + fml::RefPtr asset_manager) + : asset_manager_(asset_manager) {} + +AssetManagerFontStyleSet::~AssetManagerFontStyleSet() = default; + +void AssetManagerFontStyleSet::registerAsset(std::string asset) { + assets_.emplace_back(asset); +} + +int AssetManagerFontStyleSet::count() { + return assets_.size(); +} + +void AssetManagerFontStyleSet::getStyle(int index, + SkFontStyle*, + SkString* style) { + FXL_DCHECK(false); +} + +SkTypeface* AssetManagerFontStyleSet::createTypeface(int i) { + size_t index = i; + if (index >= assets_.size()) + return nullptr; + + TypefaceAsset& asset = assets_[index]; + if (!asset.typeface) { + std::unique_ptr> asset_buf = + std::make_unique>(); + if (!asset_manager_->GetAsBuffer(asset.asset, asset_buf.get())) + return nullptr; + + std::vector* asset_buf_ptr = asset_buf.release(); + sk_sp asset_data = + SkData::MakeWithProc(asset_buf_ptr->data(), asset_buf_ptr->size(), + VectorReleaseProc, asset_buf_ptr); + std::unique_ptr stream = SkMemoryStream::Make(asset_data); + + // Ownership of the stream is transferred. + asset.typeface = SkTypeface::MakeFromStream(stream.release()); + if (!asset.typeface) + return nullptr; + } + + return SkRef(asset.typeface.get()); +} + +SkTypeface* AssetManagerFontStyleSet::matchStyle(const SkFontStyle& pattern) { + if (assets_.empty()) + return nullptr; + + for (const TypefaceAsset& asset : assets_) + if (asset.typeface && asset.typeface->fontStyle() == pattern) + return SkRef(asset.typeface.get()); + + return SkRef(assets_[0].typeface.get()); +} + +} // namespace blink diff --git a/engine/src/flutter/lib/ui/text/asset_manager_font_provider.h b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.h new file mode 100644 index 0000000000..c0450c83cc --- /dev/null +++ b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.h @@ -0,0 +1,79 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_ +#define FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_ + +#include +#include +#include + +#include "flutter/assets/asset_manager.h" +#include "lib/fxl/macros.h" +#include "third_party/skia/include/ports/SkFontMgr.h" +#include "txt/font_asset_provider.h" + +namespace blink { + +class AssetManagerFontStyleSet : public SkFontStyleSet { + public: + AssetManagerFontStyleSet(fml::RefPtr asset_manager); + + ~AssetManagerFontStyleSet() override; + + void registerAsset(std::string asset); + + // |SkFontStyleSet| + int count() override; + + // |SkFontStyleSet| + void getStyle(int index, SkFontStyle*, SkString* style) override; + + // |SkFontStyleSet| + SkTypeface* createTypeface(int index) override; + + // |SkFontStyleSet| + SkTypeface* matchStyle(const SkFontStyle& pattern) override; + + private: + fml::RefPtr asset_manager_; + + struct TypefaceAsset { + TypefaceAsset(std::string a) : asset(std::move(a)) {} + std::string asset; + sk_sp typeface; + }; + std::vector assets_; + + FXL_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontStyleSet); +}; + +class AssetManagerFontProvider : public txt::FontAssetProvider { + public: + AssetManagerFontProvider(fml::RefPtr asset_manager); + ~AssetManagerFontProvider() override; + + void RegisterAsset(std::string family_name, std::string asset); + + // |FontAssetProvider| + size_t GetFamilyCount() const override; + + // |FontAssetProvider| + std::string GetFamilyName(int index) const override; + + // |FontAssetProvider| + SkFontStyleSet* MatchFamily(const std::string& family_name) override; + + private: + fml::RefPtr asset_manager_; + std::unordered_map + registered_families_; + std::vector family_names_; + + FXL_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontProvider); +}; + +} // namespace blink + +#endif // FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_ diff --git a/engine/src/flutter/lib/ui/text/font_collection.cc b/engine/src/flutter/lib/ui/text/font_collection.cc index d45a2d92c3..f0c0f7d699 100644 --- a/engine/src/flutter/lib/ui/text/font_collection.cc +++ b/engine/src/flutter/lib/ui/text/font_collection.cc @@ -6,13 +6,16 @@ #include +#include "flutter/lib/ui/text/asset_manager_font_provider.h" #include "flutter/runtime/test_font_data.h" #include "third_party/rapidjson/rapidjson/document.h" #include "third_party/rapidjson/rapidjson/rapidjson.h" #include "third_party/skia/include/core/SkStream.h" +#include "third_party/skia/include/core/SkTypeface.h" #include "third_party/skia/include/ports/SkFontMgr.h" #include "txt/asset_font_manager.h" #include "txt/test_font_manager.h" +#include "txt/typeface_font_asset_provider.h" namespace blink { @@ -27,9 +30,9 @@ std::shared_ptr FontCollection::GetFontCollection() const { return collection_; } -void FontCollection::RegisterFonts(const AssetManager& asset_manager) { +void FontCollection::RegisterFonts(fml::RefPtr asset_manager) { std::vector manifest_data; - if (!asset_manager.GetAsBuffer("FontManifest.json", &manifest_data)) { + if (!asset_manager->GetAsBuffer("FontManifest.json", &manifest_data)) { FXL_DLOG(WARNING) << "Could not find the font manifest in the asset store."; return; } @@ -53,7 +56,8 @@ void FontCollection::RegisterFonts(const AssetManager& asset_manager) { return; } - auto font_asset_data_provider = std::make_unique(); + auto font_provider = + std::make_unique(asset_manager); for (const auto& family : document.GetArray()) { auto family_name = family.FindMember("family"); @@ -78,37 +82,27 @@ void FontCollection::RegisterFonts(const AssetManager& asset_manager) { } // TODO: Handle weights and styles. - std::vector font_data; - if (asset_manager.GetAsBuffer(font_asset->value.GetString(), - &font_data)) { - // The data must be copied because it needs to be moved into the - // typeface as a stream. - auto data = - SkMemoryStream::MakeCopy(font_data.data(), font_data.size()); - // Ownership of the stream is transferred. - auto typeface = SkTypeface::MakeFromStream(data.release()); - font_asset_data_provider->RegisterTypeface( - std::move(typeface), family_name->value.GetString()); - } + font_provider->RegisterAsset( + family_name->value.GetString(), font_asset->value.GetString()); } } collection_->SetAssetFontManager( - sk_make_sp(std::move(font_asset_data_provider))); + sk_make_sp(std::move(font_provider))); } void FontCollection::RegisterTestFonts() { sk_sp test_typeface = SkTypeface::MakeFromStream(GetTestFontData().release()); - std::unique_ptr asset_data_provider = - std::make_unique(); + std::unique_ptr font_provider = + std::make_unique(); - asset_data_provider->RegisterTypeface(std::move(test_typeface), - GetTestFontFamilyName()); + font_provider->RegisterTypeface(std::move(test_typeface), + GetTestFontFamilyName()); collection_->SetTestFontManager(sk_make_sp( - std::move(asset_data_provider), GetTestFontFamilyName())); + std::move(font_provider), GetTestFontFamilyName())); collection_->DisableFontFallback(); } diff --git a/engine/src/flutter/lib/ui/text/font_collection.h b/engine/src/flutter/lib/ui/text/font_collection.h index 62c94ffd8d..02cca0bb51 100644 --- a/engine/src/flutter/lib/ui/text/font_collection.h +++ b/engine/src/flutter/lib/ui/text/font_collection.h @@ -23,7 +23,7 @@ class FontCollection { std::shared_ptr GetFontCollection() const; - void RegisterFonts(const AssetManager& asset_manager); + void RegisterFonts(fml::RefPtr asset_manager); void RegisterTestFonts(); diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index 8689789fa4..de3fd23489 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -89,7 +89,7 @@ bool Engine::UpdateAssetManager( if (settings_.use_test_fonts) { font_collection_.RegisterTestFonts(); } else { - font_collection_.RegisterFonts(*asset_manager_.get()); + font_collection_.RegisterFonts(asset_manager_); } return true; diff --git a/engine/src/flutter/third_party/txt/BUILD.gn b/engine/src/flutter/third_party/txt/BUILD.gn index 0cd1fff5ac..572697af4e 100644 --- a/engine/src/flutter/third_party/txt/BUILD.gn +++ b/engine/src/flutter/third_party/txt/BUILD.gn @@ -64,14 +64,9 @@ source_set("txt") { "src/minikin/SparseBitSet.h", "src/minikin/WordBreaker.cpp", "src/minikin/WordBreaker.h", - "src/txt/asset_data_provider.cc", - "src/txt/asset_data_provider.h", "src/txt/asset_font_manager.cc", "src/txt/asset_font_manager.h", - "src/txt/asset_font_style_set.cc", - "src/txt/asset_font_style_set.h", - "src/txt/directory_asset_data_provider.cc", - "src/txt/directory_asset_data_provider.h", + "src/txt/font_asset_provider.h", "src/txt/font_collection.cc", "src/txt/font_collection.h", "src/txt/font_skia.cc", @@ -96,6 +91,8 @@ source_set("txt") { "src/txt/text_decoration.h", "src/txt/text_style.cc", "src/txt/text_style.h", + "src/txt/typeface_font_asset_provider.cc", + "src/txt/typeface_font_asset_provider.h", "src/utils/JenkinsHash.cpp", "src/utils/JenkinsHash.h", "src/utils/LruCache.h", @@ -133,6 +130,19 @@ txt_common_executable_deps = [ "$flutter_root/fml", # For ICU initialization. ] +source_set("txt_test_utils") { + sources = [ + "tests/txt_test_utils.cc", + "tests/txt_test_utils.h", + ] + + deps = [ + ":txt", + "//garnet/public/lib/fxl", + "//third_party/skia", + ] +} + executable("txt_unittests") { testonly = true @@ -155,8 +165,6 @@ executable("txt_unittests") { "tests/render_test.cc", "tests/render_test.h", "tests/txt_run_all_unittests.cc", - "tests/utils.cc", - "tests/utils.h", # These tests require static fixtures. # "tests/FontCollectionItemizeTest.cpp", @@ -178,6 +186,7 @@ executable("txt_unittests") { deps = [ ":txt", + ":txt_test_utils", "//third_party/googletest:gtest", ] + txt_common_executable_deps } @@ -191,12 +200,11 @@ executable("txt_benchmarks") { "benchmarks/paragraph_builder_benchmarks.cc", "benchmarks/styled_runs_benchmarks.cc", "benchmarks/txt_run_all_benchmarks.cc", - "benchmarks/utils.cc", - "benchmarks/utils.h", ] deps = [ ":txt", + ":txt_test_utils", "//third_party/benchmark", ] + txt_common_executable_deps } diff --git a/engine/src/flutter/third_party/txt/benchmarks/paint_record_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/paint_record_benchmarks.cc index b4be01be56..732af3e96a 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/paint_record_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/paint_record_benchmarks.cc @@ -16,11 +16,11 @@ #include "third_party/benchmark/include/benchmark/benchmark_api.h" +#include "flutter/third_party/txt/tests/txt_test_utils.h" #include "lib/fxl/command_line.h" #include "lib/fxl/logging.h" #include "txt/paint_record.h" #include "txt/text_style.h" -#include "utils.h" namespace txt { diff --git a/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc index 0861f53f5b..04d21996fe 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc @@ -17,6 +17,7 @@ #include "third_party/benchmark/include/benchmark/benchmark_api.h" #include +#include "flutter/third_party/txt/tests/txt_test_utils.h" #include "lib/fxl/command_line.h" #include "lib/fxl/logging.h" #include "minikin/LayoutUtils.h" @@ -30,7 +31,6 @@ #include "txt/font_weight.h" #include "txt/paragraph.h" #include "txt/paragraph_builder.h" -#include "utils.h" namespace txt { diff --git a/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc index 6030c0e342..2004889cee 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc @@ -16,6 +16,7 @@ #include "third_party/benchmark/include/benchmark/benchmark_api.h" +#include "flutter/third_party/txt/tests/txt_test_utils.h" #include "lib/fxl/logging.h" #include "third_party/icu/source/common/unicode/unistr.h" #include "third_party/skia/include/core/SkColor.h" @@ -24,7 +25,6 @@ #include "txt/font_weight.h" #include "txt/paragraph.h" #include "txt/paragraph_builder.h" -#include "utils.h" namespace txt { diff --git a/engine/src/flutter/third_party/txt/benchmarks/styled_runs_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/styled_runs_benchmarks.cc index a0e21fc891..c3300ac79a 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/styled_runs_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/styled_runs_benchmarks.cc @@ -16,11 +16,11 @@ #include "third_party/benchmark/include/benchmark/benchmark_api.h" +#include "flutter/third_party/txt/tests/txt_test_utils.h" #include "lib/fxl/command_line.h" #include "lib/fxl/logging.h" #include "txt/styled_runs.h" #include "txt/text_style.h" -#include "utils.h" namespace txt { diff --git a/engine/src/flutter/third_party/txt/benchmarks/txt_run_all_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/txt_run_all_benchmarks.cc index 98b6c68fa6..b5c0bbeadc 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/txt_run_all_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/txt_run_all_benchmarks.cc @@ -17,8 +17,8 @@ #include "third_party/benchmark/include/benchmark/benchmark_api.h" #include "flutter/fml/icu_util.h" +#include "flutter/third_party/txt/tests/txt_test_utils.h" #include "lib/fxl/logging.h" -#include "utils.h" // We will use a custom main to allow custom font directories for consistency. int main(int argc, char** argv) { diff --git a/engine/src/flutter/third_party/txt/benchmarks/utils.cc b/engine/src/flutter/third_party/txt/benchmarks/utils.cc deleted file mode 100644 index f5164f2038..0000000000 --- a/engine/src/flutter/third_party/txt/benchmarks/utils.cc +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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/fxl/command_line.h" -#include "txt/directory_asset_data_provider.h" -#include "txt/font_collection.h" -#include "utils.h" - -namespace txt { - -static std::string gFontDir; -static fxl::CommandLine gCommandLine; - -const std::string& GetFontDir() { - return gFontDir; -} - -void SetFontDir(const std::string& dir) { - gFontDir = dir; -} - -const fxl::CommandLine& GetCommandLineForProcess() { - return gCommandLine; -} - -void SetCommandLine(fxl::CommandLine cmd) { - gCommandLine = std::move(cmd); -} - -std::shared_ptr GetTestFontCollection() { - auto collection = std::make_shared(); - collection->SetAssetFontManager(sk_make_sp( - std::make_unique(GetFontDir()))); - return collection; -} - -} // namespace txt diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_data_provider.cc b/engine/src/flutter/third_party/txt/src/txt/asset_data_provider.cc deleted file mode 100644 index fa35cc0853..0000000000 --- a/engine/src/flutter/third_party/txt/src/txt/asset_data_provider.cc +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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 "txt/asset_data_provider.h" -#include "lib/fxl/logging.h" - -namespace txt { - -AssetDataProvider::AssetDataProvider() = default; - -AssetDataProvider::~AssetDataProvider() = default; - -size_t AssetDataProvider::GetFamilyCount() const { - return family_names_.size(); -} - -const std::string& AssetDataProvider::GetFamilyName(int index) const { - return family_names_[index]; -} - -AssetFontStyleSet* AssetDataProvider::MatchFamily( - const std::string& family_name) { - auto found = registered_families_.find(family_name); - if (found == registered_families_.end()) { - return nullptr; - } - return SkRef(&found->second); -} - -void AssetDataProvider::RegisterTypeface(sk_sp typeface) { - if (typeface == nullptr) { - return; - } - - SkString sk_family_name; - typeface->getFamilyName(&sk_family_name); - - std::string family_name(sk_family_name.c_str(), sk_family_name.size()); - RegisterTypeface(std::move(typeface), std::move(family_name)); -} - -void AssetDataProvider::RegisterTypeface(sk_sp typeface, - std::string family_name_alias) { - if (family_name_alias.empty()) { - return; - } - - auto family_it = registered_families_.find(family_name_alias); - if (family_it == registered_families_.end()) { - family_names_.push_back(family_name_alias); - family_it = registered_families_ - .emplace(std::piecewise_construct, - std::forward_as_tuple(family_name_alias), - std::forward_as_tuple()) - .first; - } - family_it->second.registerTypeface(std::move(typeface)); -} - -} // namespace txt diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_data_provider.h b/engine/src/flutter/third_party/txt/src/txt/asset_data_provider.h deleted file mode 100644 index 78659339f1..0000000000 --- a/engine/src/flutter/third_party/txt/src/txt/asset_data_provider.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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. - */ - -#ifndef TXT_ASSET_DATA_PROVIDER_H_ -#define TXT_ASSET_DATA_PROVIDER_H_ - -#include -#include -#include "lib/fxl/macros.h" -#include "third_party/skia/include/core/SkTypeface.h" -#include "txt/asset_font_style_set.h" - -namespace txt { - -class AssetDataProvider { - public: - AssetDataProvider(); - - virtual ~AssetDataProvider(); - - size_t GetFamilyCount() const; - - const std::string& GetFamilyName(int index) const; - - AssetFontStyleSet* MatchFamily(const std::string& family_name); - - void RegisterTypeface(sk_sp typeface); - - void RegisterTypeface(sk_sp typeface, - std::string family_name_alias); - - private: - std::unordered_map registered_families_; - std::vector family_names_; - - FXL_DISALLOW_COPY_AND_ASSIGN(AssetDataProvider); -}; - -} // namespace txt - -#endif // TXT_ASSET_DATA_PROVIDER_H_ diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc index e0af5f9302..c9edd2b241 100644 --- a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc +++ b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc @@ -15,25 +15,29 @@ */ #include "txt/asset_font_manager.h" + #include + #include "lib/fxl/logging.h" +#include "third_party/skia/include/core/SkString.h" +#include "third_party/skia/include/core/SkTypeface.h" namespace txt { AssetFontManager::AssetFontManager( - std::unique_ptr data_provider) - : data_provider_(std::move(data_provider)) { - FXL_DCHECK(data_provider_ != nullptr); + std::unique_ptr font_provider) + : font_provider_(std::move(font_provider)) { + FXL_DCHECK(font_provider_ != nullptr); } AssetFontManager::~AssetFontManager() = default; int AssetFontManager::onCountFamilies() const { - return data_provider_->GetFamilyCount(); + return font_provider_->GetFamilyCount(); } void AssetFontManager::onGetFamilyName(int index, SkString* familyName) const { - familyName->set(data_provider_->GetFamilyName(index).c_str()); + familyName->set(font_provider_->GetFamilyName(index).c_str()); } SkFontStyleSet* AssetFontManager::onCreateStyleSet(int index) const { @@ -44,14 +48,14 @@ SkFontStyleSet* AssetFontManager::onCreateStyleSet(int index) const { SkFontStyleSet* AssetFontManager::onMatchFamily( const char family_name_string[]) const { std::string family_name(family_name_string); - return data_provider_->MatchFamily(family_name); + return font_provider_->MatchFamily(family_name); } SkTypeface* AssetFontManager::onMatchFamilyStyle( const char familyName[], const SkFontStyle& style) const { SkFontStyleSet* font_style_set = - data_provider_->MatchFamily(std::string(familyName)); + font_provider_->MatchFamily(std::string(familyName)); if (font_style_set == nullptr) return nullptr; return font_style_set->matchStyle(style); diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h index 646caf3446..c6d0bc1b2f 100644 --- a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h +++ b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h @@ -21,13 +21,13 @@ #include "lib/fxl/macros.h" #include "third_party/skia/include/core/SkStream.h" #include "third_party/skia/include/ports/SkFontMgr.h" -#include "txt/asset_data_provider.h" +#include "txt/font_asset_provider.h" namespace txt { class AssetFontManager : public SkFontMgr { public: - AssetFontManager(std::unique_ptr data_provider); + AssetFontManager(std::unique_ptr font_provider); ~AssetFontManager() override; @@ -36,7 +36,7 @@ class AssetFontManager : public SkFontMgr { SkFontStyleSet* onMatchFamily(const char familyName[]) const override; private: - std::unique_ptr data_provider_; + std::unique_ptr font_provider_; // |SkFontMgr| int onCountFamilies() const override; diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.cc b/engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.cc deleted file mode 100644 index c0ebcf78e2..0000000000 --- a/engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2017 The Chromium 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/asset_font_style_set.h" -#include "lib/fxl/logging.h" - -namespace txt { - -AssetFontStyleSet::AssetFontStyleSet() = default; - -AssetFontStyleSet::~AssetFontStyleSet() = default; - -void AssetFontStyleSet::registerTypeface(sk_sp typeface) { - if (typeface == nullptr) { - return; - } - typefaces_.emplace_back(std::move(typeface)); -} - -int AssetFontStyleSet::count() { - return typefaces_.size(); -} - -void AssetFontStyleSet::getStyle(int index, SkFontStyle*, SkString* style) { - FXL_DCHECK(false); -} - -SkTypeface* AssetFontStyleSet::createTypeface(int index) { - auto index_cast = static_cast(index); - if (index_cast >= typefaces_.size()) { - return nullptr; - } - return SkRef(typefaces_[index_cast].get()); -} - -SkTypeface* AssetFontStyleSet::matchStyle(const SkFontStyle& pattern) { - if (typefaces_.empty()) - return nullptr; - - for (const sk_sp& typeface : typefaces_) - if (typeface->fontStyle() == pattern) - return typeface.get(); - - return SkRef(typefaces_[0].get()); -} - -} // namespace txt diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.h b/engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.h deleted file mode 100644 index 23c610917c..0000000000 --- a/engine/src/flutter/third_party/txt/src/txt/asset_font_style_set.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2017 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef TXT_ASSET_FONT_STYLE_SET_H_ -#define TXT_ASSET_FONT_STYLE_SET_H_ - -#include -#include "lib/fxl/macros.h" -#include "third_party/skia/include/core/SkFontStyle.h" -#include "third_party/skia/include/core/SkTypeface.h" -#include "third_party/skia/include/ports/SkFontMgr.h" - -namespace txt { - -class AssetFontStyleSet : public SkFontStyleSet { - public: - AssetFontStyleSet(); - - ~AssetFontStyleSet() override; - - void registerTypeface(sk_sp typeface); - - // |SkFontStyleSet| - int count() override; - - // |SkFontStyleSet| - void getStyle(int index, SkFontStyle*, SkString* style) override; - - // |SkFontStyleSet| - SkTypeface* createTypeface(int index) override; - - // |SkFontStyleSet| - SkTypeface* matchStyle(const SkFontStyle& pattern) override; - - private: - std::vector> typefaces_; - - FXL_DISALLOW_COPY_AND_ASSIGN(AssetFontStyleSet); -}; - -} // namespace txt - -#endif // TXT_ASSET_FONT_STYLE_SET_H_ diff --git a/engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.cc b/engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.cc deleted file mode 100644 index 0a78bcb95c..0000000000 --- a/engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.cc +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2017 The Chromium 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/directory_asset_data_provider.h" -#include -#include "lib/fxl/logging.h" -#include "third_party/skia/include/core/SkStream.h" -#include "third_party/skia/include/core/SkTypeface.h" -#include "utils/WindowsUtils.h" - -#if !defined(_WIN32) -#include -#endif - -namespace txt { - -DirectoryAssetDataProvider::DirectoryAssetDataProvider( - const std::string& directory_path) { -#if defined(_WIN32) - std::string path = directory_path + "\\*"; - WIN32_FIND_DATAA ffd; - HANDLE directory = FindFirstFileA(path.c_str(), &ffd); - if (directory == INVALID_HANDLE_VALUE) { - return; - } - - do { - if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { - continue; - } - - std::string file_name(ffd.cFileName); - - std::stringstream file_path; - file_path << directory_path << "/" << file_name; - - RegisterTypeface(SkTypeface::MakeFromFile(file_path.str().c_str())); - } while (FindNextFileA(directory, &ffd) != 0); - - // TODO(bkonyi): check for error here? - FindClose(directory); -#else - auto directory_closer = [](DIR* directory) { - if (directory != nullptr) { - ::closedir(directory); - } - }; - - std::unique_ptr directory( - ::opendir(directory_path.c_str()), directory_closer); - - if (directory == nullptr) { - return; - } - - for (struct dirent* entry = ::readdir(directory.get()); entry != nullptr; - entry = ::readdir(directory.get())) { - if (entry->d_type != DT_REG) { - continue; - } - - std::string file_name(entry->d_name); - - std::stringstream file_path; - file_path << directory_path << "/" << file_name; - - RegisterTypeface(SkTypeface::MakeFromFile(file_path.str().c_str())); - } -#endif -} - -DirectoryAssetDataProvider::~DirectoryAssetDataProvider() = default; - -} // namespace txt diff --git a/engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.h b/engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.h deleted file mode 100644 index 7f0a751008..0000000000 --- a/engine/src/flutter/third_party/txt/src/txt/directory_asset_data_provider.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2017 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef TXT_DIRECTORY_ASSET_DATA_PROVIDER_H_ -#define TXT_DIRECTORY_ASSET_DATA_PROVIDER_H_ - -#include "lib/fxl/macros.h" -#include "txt/asset_data_provider.h" - -namespace txt { - -class DirectoryAssetDataProvider : public AssetDataProvider { - public: - DirectoryAssetDataProvider(const std::string& directory); - - ~DirectoryAssetDataProvider() override; - - private: - FXL_DISALLOW_COPY_AND_ASSIGN(DirectoryAssetDataProvider); -}; - -} // namespace txt - -#endif // TXT_DIRECTORY_ASSET_DATA_PROVIDER_H_ diff --git a/engine/src/flutter/third_party/txt/tests/utils.cc b/engine/src/flutter/third_party/txt/src/txt/font_asset_provider.h similarity index 57% rename from engine/src/flutter/third_party/txt/tests/utils.cc rename to engine/src/flutter/third_party/txt/src/txt/font_asset_provider.h index 79e5bdc0b9..397ed2ec6e 100644 --- a/engine/src/flutter/third_party/txt/tests/utils.cc +++ b/engine/src/flutter/third_party/txt/src/txt/font_asset_provider.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google, Inc. + * Copyright 2018 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,30 +14,22 @@ * limitations under the License. */ -#include +#ifndef TXT_FONT_ASSET_PROVIDER_H_ +#define TXT_FONT_ASSET_PROVIDER_H_ -#include "lib/fxl/command_line.h" -#include "utils.h" +#include "third_party/skia/include/ports/SkFontMgr.h" namespace txt { -static std::string gFontDir; -static fxl::CommandLine gCommandLine; +class FontAssetProvider { + public: + virtual ~FontAssetProvider() = default; -const std::string& GetFontDir() { - return gFontDir; -} - -void SetFontDir(const std::string& dir) { - gFontDir = dir; -} - -const fxl::CommandLine& GetCommandLineForProcess() { - return gCommandLine; -} - -void SetCommandLine(fxl::CommandLine cmd) { - gCommandLine = std::move(cmd); -} + virtual size_t GetFamilyCount() const = 0; + virtual std::string GetFamilyName(int index) const = 0; + virtual SkFontStyleSet* MatchFamily(const std::string& family_name) = 0; +}; } // namespace txt + +#endif // TXT_FONT_ASSET_PROVIDER_H_ diff --git a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc index db68b938ab..d5ce63da79 100644 --- a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc +++ b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc @@ -20,9 +20,9 @@ namespace txt { TestFontManager::TestFontManager( - std::unique_ptr data_provider, + std::unique_ptr font_provider, std::string test_font_family_name) - : AssetFontManager(std::move(data_provider)), + : AssetFontManager(std::move(font_provider)), test_font_family_name_(test_font_family_name) {} TestFontManager::~TestFontManager() = default; diff --git a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h index 45dd626402..dd4f2c32be 100644 --- a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h +++ b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h @@ -22,8 +22,8 @@ #include "lib/fxl/macros.h" #include "third_party/skia/include/ports/SkFontMgr.h" -#include "txt/asset_data_provider.h" #include "txt/asset_font_manager.h" +#include "txt/font_asset_provider.h" namespace txt { @@ -31,7 +31,7 @@ namespace txt { // one family. class TestFontManager : public AssetFontManager { public: - TestFontManager(std::unique_ptr data_provider, + TestFontManager(std::unique_ptr font_provider, std::string test_font_family_name); ~TestFontManager() override; diff --git a/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc new file mode 100644 index 0000000000..a57af7d9d6 --- /dev/null +++ b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc @@ -0,0 +1,118 @@ +/* + * Copyright 2018 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 "txt/typeface_font_asset_provider.h" + +#include "lib/fxl/logging.h" +#include "third_party/skia/include/core/SkString.h" +#include "third_party/skia/include/core/SkTypeface.h" + +namespace txt { + +TypefaceFontAssetProvider::TypefaceFontAssetProvider() = default; + +TypefaceFontAssetProvider::~TypefaceFontAssetProvider() = default; + +// |FontAssetProvider| +size_t TypefaceFontAssetProvider::GetFamilyCount() const { + return family_names_.size(); +} + +// |FontAssetProvider| +std::string TypefaceFontAssetProvider::GetFamilyName(int index) const { + return family_names_[index]; +} + +// |FontAssetProvider| +SkFontStyleSet* TypefaceFontAssetProvider::MatchFamily( + const std::string& family_name) { + auto found = registered_families_.find(family_name); + if (found == registered_families_.end()) { + return nullptr; + } + return SkRef(&found->second); +} + +void TypefaceFontAssetProvider::RegisterTypeface(sk_sp typeface) { + if (typeface == nullptr) { + return; + } + + SkString sk_family_name; + typeface->getFamilyName(&sk_family_name); + + std::string family_name(sk_family_name.c_str(), sk_family_name.size()); + RegisterTypeface(std::move(typeface), std::move(family_name)); +} + +void TypefaceFontAssetProvider::RegisterTypeface( + sk_sp typeface, + std::string family_name_alias) { + if (family_name_alias.empty()) { + return; + } + + auto family_it = registered_families_.find(family_name_alias); + if (family_it == registered_families_.end()) { + family_names_.push_back(family_name_alias); + family_it = registered_families_ + .emplace(std::piecewise_construct, + std::forward_as_tuple(family_name_alias), + std::forward_as_tuple()) + .first; + } + family_it->second.registerTypeface(std::move(typeface)); +} + +TypefaceFontStyleSet::TypefaceFontStyleSet() = default; + +TypefaceFontStyleSet::~TypefaceFontStyleSet() = default; + +void TypefaceFontStyleSet::registerTypeface(sk_sp typeface) { + if (typeface == nullptr) { + return; + } + typefaces_.emplace_back(std::move(typeface)); +} + +int TypefaceFontStyleSet::count() { + return typefaces_.size(); +} + +void TypefaceFontStyleSet::getStyle(int index, SkFontStyle*, SkString* style) { + FXL_DCHECK(false); +} + +SkTypeface* TypefaceFontStyleSet::createTypeface(int i) { + size_t index = i; + if (index >= typefaces_.size()) { + return nullptr; + } + return SkRef(typefaces_[index].get()); +} + +SkTypeface* TypefaceFontStyleSet::matchStyle(const SkFontStyle& pattern) { + if (typefaces_.empty()) + return nullptr; + + for (const sk_sp& typeface : typefaces_) + if (typeface->fontStyle() == pattern) + return SkRef(typeface.get()); + + return SkRef(typefaces_[0].get()); +} + +} // namespace txt diff --git a/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h new file mode 100644 index 0000000000..f41b5b7c34 --- /dev/null +++ b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h @@ -0,0 +1,84 @@ +/* + * Copyright 2018 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. + */ + +#ifndef TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_ +#define TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_ + +#include +#include +#include + +#include "lib/fxl/macros.h" +#include "third_party/skia/include/ports/SkFontMgr.h" +#include "txt/font_asset_provider.h" + +namespace txt { + +class TypefaceFontStyleSet : public SkFontStyleSet { + public: + TypefaceFontStyleSet(); + + ~TypefaceFontStyleSet() override; + + void registerTypeface(sk_sp typeface); + + // |SkFontStyleSet| + int count() override; + + // |SkFontStyleSet| + void getStyle(int index, SkFontStyle*, SkString* style) override; + + // |SkFontStyleSet| + SkTypeface* createTypeface(int index) override; + + // |SkFontStyleSet| + SkTypeface* matchStyle(const SkFontStyle& pattern) override; + + private: + std::vector> typefaces_; + + FXL_DISALLOW_COPY_AND_ASSIGN(TypefaceFontStyleSet); +}; + +class TypefaceFontAssetProvider : public FontAssetProvider { + public: + TypefaceFontAssetProvider(); + ~TypefaceFontAssetProvider() override; + + void RegisterTypeface(sk_sp typeface); + + void RegisterTypeface(sk_sp typeface, + std::string family_name_alias); + + // |FontAssetProvider| + size_t GetFamilyCount() const override; + + // |FontAssetProvider| + std::string GetFamilyName(int index) const override; + + // |FontAssetProvider| + SkFontStyleSet* MatchFamily(const std::string& family_name) override; + + private: + std::unordered_map registered_families_; + std::vector family_names_; + + FXL_DISALLOW_COPY_AND_ASSIGN(TypefaceFontAssetProvider); +}; + +} // namespace txt + +#endif // TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_ diff --git a/engine/src/flutter/third_party/txt/tests/font_collection_unittests.cc b/engine/src/flutter/third_party/txt/tests/font_collection_unittests.cc index 0b8c4856aa..fc81ad281c 100644 --- a/engine/src/flutter/third_party/txt/tests/font_collection_unittests.cc +++ b/engine/src/flutter/third_party/txt/tests/font_collection_unittests.cc @@ -18,7 +18,7 @@ #include "lib/fxl/command_line.h" #include "lib/fxl/logging.h" #include "txt/font_collection.h" -#include "utils.h" +#include "txt_test_utils.h" namespace txt { diff --git a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc index 13027f90d4..30c29a62c7 100644 --- a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc +++ b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc @@ -22,7 +22,7 @@ #include "txt/font_weight.h" #include "txt/paragraph.h" #include "txt/paragraph_builder.h" -#include "utils.h" +#include "txt_test_utils.h" #define DISABLE_ON_WINDOWS(TEST) DISABLE_TEST_WINDOWS(TEST) diff --git a/engine/src/flutter/third_party/txt/tests/render_test.cc b/engine/src/flutter/third_party/txt/tests/render_test.cc index 802ebbfd6c..6b4bde573d 100644 --- a/engine/src/flutter/third_party/txt/tests/render_test.cc +++ b/engine/src/flutter/third_party/txt/tests/render_test.cc @@ -22,17 +22,13 @@ #include "third_party/skia/include/core/SkImageEncoder.h" #include "third_party/skia/include/core/SkStream.h" #include "txt/asset_font_manager.h" -#include "txt/directory_asset_data_provider.h" #include "txt/font_collection.h" -#include "utils.h" +#include "txt_test_utils.h" namespace txt { RenderTest::RenderTest() - : snapshots_(0), font_collection_(std::make_shared()) { - font_collection_->SetAssetFontManager(sk_make_sp( - std::make_unique(GetFontDir()))); -} + : snapshots_(0), font_collection_(GetTestFontCollection()) {} RenderTest::~RenderTest() = default; diff --git a/engine/src/flutter/third_party/txt/tests/txt_run_all_unittests.cc b/engine/src/flutter/third_party/txt/tests/txt_run_all_unittests.cc index 5a059c02db..a712566140 100644 --- a/engine/src/flutter/third_party/txt/tests/txt_run_all_unittests.cc +++ b/engine/src/flutter/third_party/txt/tests/txt_run_all_unittests.cc @@ -19,7 +19,7 @@ #include "lib/fxl/command_line.h" #include "lib/fxl/logging.h" #include "third_party/skia/include/core/SkGraphics.h" -#include "utils.h" +#include "txt_test_utils.h" #include diff --git a/engine/src/flutter/third_party/txt/tests/txt_test_utils.cc b/engine/src/flutter/third_party/txt/tests/txt_test_utils.cc new file mode 100644 index 0000000000..da90b9cb73 --- /dev/null +++ b/engine/src/flutter/third_party/txt/tests/txt_test_utils.cc @@ -0,0 +1,119 @@ +/* + * 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 "txt_test_utils.h" + +#include "third_party/skia/include/core/SkTypeface.h" +#include "txt/asset_font_manager.h" +#include "txt/typeface_font_asset_provider.h" +#include "utils/WindowsUtils.h" + +#if !defined(_WIN32) +#include +#endif + +namespace txt { + +static std::string gFontDir; +static fxl::CommandLine gCommandLine; + +const std::string& GetFontDir() { + return gFontDir; +} + +void SetFontDir(const std::string& dir) { + gFontDir = dir; +} + +const fxl::CommandLine& GetCommandLineForProcess() { + return gCommandLine; +} + +void SetCommandLine(fxl::CommandLine cmd) { + gCommandLine = std::move(cmd); +} + +void RegisterFontsFromPath(TypefaceFontAssetProvider& font_provider, + std::string directory_path) { +#if defined(_WIN32) + std::string path = directory_path + "\\*"; + WIN32_FIND_DATAA ffd; + HANDLE directory = FindFirstFileA(path.c_str(), &ffd); + if (directory == INVALID_HANDLE_VALUE) { + return; + } + + do { + if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { + continue; + } + + std::string file_name(ffd.cFileName); + + std::stringstream file_path; + file_path << directory_path << "/" << file_name; + + font_provider.RegisterTypeface( + SkTypeface::MakeFromFile(file_path.str().c_str())); + } while (FindNextFileA(directory, &ffd) != 0); + + // TODO(bkonyi): check for error here? + FindClose(directory); +#else + auto directory_closer = [](DIR* directory) { + if (directory != nullptr) { + ::closedir(directory); + } + }; + + std::unique_ptr directory( + ::opendir(directory_path.c_str()), directory_closer); + + if (directory == nullptr) { + return; + } + + for (struct dirent* entry = ::readdir(directory.get()); entry != nullptr; + entry = ::readdir(directory.get())) { + if (entry->d_type != DT_REG) { + continue; + } + + std::string file_name(entry->d_name); + + std::stringstream file_path; + file_path << directory_path << "/" << file_name; + + font_provider.RegisterTypeface( + SkTypeface::MakeFromFile(file_path.str().c_str())); + } +#endif +} + +std::shared_ptr GetTestFontCollection() { + std::unique_ptr font_provider = + std::make_unique(); + RegisterFontsFromPath(*font_provider, GetFontDir()); + + std::shared_ptr collection = + std::make_shared(); + collection->SetAssetFontManager( + sk_make_sp(std::move(font_provider))); + + return collection; +} + +} // namespace txt diff --git a/engine/src/flutter/third_party/txt/benchmarks/utils.h b/engine/src/flutter/third_party/txt/tests/txt_test_utils.h similarity index 95% rename from engine/src/flutter/third_party/txt/benchmarks/utils.h rename to engine/src/flutter/third_party/txt/tests/txt_test_utils.h index e75929a524..b69f3fa984 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/utils.h +++ b/engine/src/flutter/third_party/txt/tests/txt_test_utils.h @@ -14,15 +14,13 @@ * limitations under the License. */ -#include #include #include "lib/fxl/command_line.h" +#include "txt/font_collection.h" namespace txt { -class FontCollection; - const std::string& GetFontDir(); void SetFontDir(const std::string& dir); diff --git a/engine/src/flutter/third_party/txt/tests/utils.h b/engine/src/flutter/third_party/txt/tests/utils.h deleted file mode 100644 index 5e6286a364..0000000000 --- a/engine/src/flutter/third_party/txt/tests/utils.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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/fxl/command_line.h" - -namespace txt { - -const std::string& GetFontDir(); - -void SetFontDir(const std::string& dir); - -const fxl::CommandLine& GetCommandLineForProcess(); - -void SetCommandLine(fxl::CommandLine cmd); - -} // namespace txt