未验证 提交 c3c40cf9 编写于 作者: J Jason Simmons 提交者: GitHub

Lazily load fonts that are embedded within the application (#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.
上级 b8b2ff34
......@@ -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",
......
// 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<std::vector<uint8_t>*>(context);
}
} // anonymous namespace
AssetManagerFontProvider::AssetManagerFontProvider(
fml::RefPtr<blink::AssetManager> 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<size_t>(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<blink::AssetManager> 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<std::vector<uint8_t>> asset_buf =
std::make_unique<std::vector<uint8_t>>();
if (!asset_manager_->GetAsBuffer(asset.asset, asset_buf.get()))
return nullptr;
std::vector<uint8_t>* asset_buf_ptr = asset_buf.release();
sk_sp<SkData> asset_data =
SkData::MakeWithProc(asset_buf_ptr->data(), asset_buf_ptr->size(),
VectorReleaseProc, asset_buf_ptr);
std::unique_ptr<SkMemoryStream> 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
// 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 <string>
#include <unordered_map>
#include <vector>
#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<blink::AssetManager> 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<blink::AssetManager> asset_manager_;
struct TypefaceAsset {
TypefaceAsset(std::string a) : asset(std::move(a)) {}
std::string asset;
sk_sp<SkTypeface> typeface;
};
std::vector<TypefaceAsset> assets_;
FXL_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontStyleSet);
};
class AssetManagerFontProvider : public txt::FontAssetProvider {
public:
AssetManagerFontProvider(fml::RefPtr<blink::AssetManager> 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<AssetManager> asset_manager_;
std::unordered_map<std::string, AssetManagerFontStyleSet>
registered_families_;
std::vector<std::string> family_names_;
FXL_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontProvider);
};
} // namespace blink
#endif // FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_
......@@ -6,13 +6,16 @@
#include <mutex>
#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<txt::FontCollection> FontCollection::GetFontCollection() const {
return collection_;
}
void FontCollection::RegisterFonts(const AssetManager& asset_manager) {
void FontCollection::RegisterFonts(fml::RefPtr<AssetManager> asset_manager) {
std::vector<uint8_t> 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<txt::AssetDataProvider>();
auto font_provider =
std::make_unique<AssetManagerFontProvider>(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<uint8_t> 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<txt::AssetFontManager>(std::move(font_asset_data_provider)));
sk_make_sp<txt::AssetFontManager>(std::move(font_provider)));
}
void FontCollection::RegisterTestFonts() {
sk_sp<SkTypeface> test_typeface =
SkTypeface::MakeFromStream(GetTestFontData().release());
std::unique_ptr<txt::AssetDataProvider> asset_data_provider =
std::make_unique<txt::AssetDataProvider>();
std::unique_ptr<txt::TypefaceFontAssetProvider> font_provider =
std::make_unique<txt::TypefaceFontAssetProvider>();
asset_data_provider->RegisterTypeface(std::move(test_typeface),
GetTestFontFamilyName());
font_provider->RegisterTypeface(std::move(test_typeface),
GetTestFontFamilyName());
collection_->SetTestFontManager(sk_make_sp<txt::TestFontManager>(
std::move(asset_data_provider), GetTestFontFamilyName()));
std::move(font_provider), GetTestFontFamilyName()));
collection_->DisableFontFallback();
}
......
......@@ -23,7 +23,7 @@ class FontCollection {
std::shared_ptr<txt::FontCollection> GetFontCollection() const;
void RegisterFonts(const AssetManager& asset_manager);
void RegisterFonts(fml::RefPtr<AssetManager> asset_manager);
void RegisterTestFonts();
......
......@@ -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;
......
......@@ -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
}
......@@ -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 {
......
......@@ -17,6 +17,7 @@
#include "third_party/benchmark/include/benchmark/benchmark_api.h"
#include <minikin/Layout.h>
#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 {
......
......@@ -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 {
......
......@@ -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 {
......
......@@ -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) {
......
/*
* 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 <string>
#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<FontCollection> GetTestFontCollection() {
auto collection = std::make_shared<FontCollection>();
collection->SetAssetFontManager(sk_make_sp<AssetFontManager>(
std::make_unique<DirectoryAssetDataProvider>(GetFontDir())));
return collection;
}
} // namespace txt
......@@ -15,25 +15,29 @@
*/
#include "txt/asset_font_manager.h"
#include <memory>
#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<AssetDataProvider> data_provider)
: data_provider_(std::move(data_provider)) {
FXL_DCHECK(data_provider_ != nullptr);
std::unique_ptr<FontAssetProvider> 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);
......
......@@ -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<AssetDataProvider> data_provider);
AssetFontManager(std::unique_ptr<FontAssetProvider> font_provider);
~AssetFontManager() override;
......@@ -36,7 +36,7 @@ class AssetFontManager : public SkFontMgr {
SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
private:
std::unique_ptr<AssetDataProvider> data_provider_;
std::unique_ptr<FontAssetProvider> font_provider_;
// |SkFontMgr|
int onCountFamilies() const override;
......
// 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<SkTypeface> 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<size_t>(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<SkTypeface>& typeface : typefaces_)
if (typeface->fontStyle() == pattern)
return typeface.get();
return SkRef(typefaces_[0].get());
}
} // namespace txt
// 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 <vector>
#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<SkTypeface> 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<sk_sp<SkTypeface>> typefaces_;
FXL_DISALLOW_COPY_AND_ASSIGN(AssetFontStyleSet);
};
} // namespace txt
#endif // TXT_ASSET_FONT_STYLE_SET_H_
// 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_
/*
* 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 <string>
#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_
......@@ -20,9 +20,9 @@
namespace txt {
TestFontManager::TestFontManager(
std::unique_ptr<AssetDataProvider> data_provider,
std::unique_ptr<FontAssetProvider> 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;
......
......@@ -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<AssetDataProvider> data_provider,
TestFontManager(std::unique_ptr<FontAssetProvider> font_provider,
std::string test_font_family_name);
~TestFontManager() override;
......
/*
* 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,24 +14,30 @@
* limitations under the License.
*/
#include "txt/asset_data_provider.h"
#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 {
AssetDataProvider::AssetDataProvider() = default;
TypefaceFontAssetProvider::TypefaceFontAssetProvider() = default;
AssetDataProvider::~AssetDataProvider() = default;
TypefaceFontAssetProvider::~TypefaceFontAssetProvider() = default;
size_t AssetDataProvider::GetFamilyCount() const {
// |FontAssetProvider|
size_t TypefaceFontAssetProvider::GetFamilyCount() const {
return family_names_.size();
}
const std::string& AssetDataProvider::GetFamilyName(int index) const {
// |FontAssetProvider|
std::string TypefaceFontAssetProvider::GetFamilyName(int index) const {
return family_names_[index];
}
AssetFontStyleSet* AssetDataProvider::MatchFamily(
// |FontAssetProvider|
SkFontStyleSet* TypefaceFontAssetProvider::MatchFamily(
const std::string& family_name) {
auto found = registered_families_.find(family_name);
if (found == registered_families_.end()) {
......@@ -40,7 +46,7 @@ AssetFontStyleSet* AssetDataProvider::MatchFamily(
return SkRef(&found->second);
}
void AssetDataProvider::RegisterTypeface(sk_sp<SkTypeface> typeface) {
void TypefaceFontAssetProvider::RegisterTypeface(sk_sp<SkTypeface> typeface) {
if (typeface == nullptr) {
return;
}
......@@ -52,8 +58,9 @@ void AssetDataProvider::RegisterTypeface(sk_sp<SkTypeface> typeface) {
RegisterTypeface(std::move(typeface), std::move(family_name));
}
void AssetDataProvider::RegisterTypeface(sk_sp<SkTypeface> typeface,
std::string family_name_alias) {
void TypefaceFontAssetProvider::RegisterTypeface(
sk_sp<SkTypeface> typeface,
std::string family_name_alias) {
if (family_name_alias.empty()) {
return;
}
......@@ -70,4 +77,42 @@ void AssetDataProvider::RegisterTypeface(sk_sp<SkTypeface> typeface,
family_it->second.registerTypeface(std::move(typeface));
}
TypefaceFontStyleSet::TypefaceFontStyleSet() = default;
TypefaceFontStyleSet::~TypefaceFontStyleSet() = default;
void TypefaceFontStyleSet::registerTypeface(sk_sp<SkTypeface> 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<SkTypeface>& typeface : typefaces_)
if (typeface->fontStyle() == pattern)
return SkRef(typeface.get());
return SkRef(typefaces_[0].get());
}
} // namespace txt
/*
* 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,41 +14,71 @@
* limitations under the License.
*/
#ifndef TXT_ASSET_DATA_PROVIDER_H_
#define TXT_ASSET_DATA_PROVIDER_H_
#ifndef TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_
#define TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_
#include <string>
#include <unordered_map>
#include <vector>
#include "lib/fxl/macros.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "txt/asset_font_style_set.h"
#include "third_party/skia/include/ports/SkFontMgr.h"
#include "txt/font_asset_provider.h"
namespace txt {
class AssetDataProvider {
class TypefaceFontStyleSet : public SkFontStyleSet {
public:
AssetDataProvider();
TypefaceFontStyleSet();
~TypefaceFontStyleSet() override;
void registerTypeface(sk_sp<SkTypeface> typeface);
// |SkFontStyleSet|
int count() override;
// |SkFontStyleSet|
void getStyle(int index, SkFontStyle*, SkString* style) override;
// |SkFontStyleSet|
SkTypeface* createTypeface(int index) override;
virtual ~AssetDataProvider();
// |SkFontStyleSet|
SkTypeface* matchStyle(const SkFontStyle& pattern) override;
size_t GetFamilyCount() const;
private:
std::vector<sk_sp<SkTypeface>> typefaces_;
const std::string& GetFamilyName(int index) const;
FXL_DISALLOW_COPY_AND_ASSIGN(TypefaceFontStyleSet);
};
AssetFontStyleSet* MatchFamily(const std::string& family_name);
class TypefaceFontAssetProvider : public FontAssetProvider {
public:
TypefaceFontAssetProvider();
~TypefaceFontAssetProvider() override;
void RegisterTypeface(sk_sp<SkTypeface> typeface);
void RegisterTypeface(sk_sp<SkTypeface> 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<std::string, AssetFontStyleSet> registered_families_;
std::unordered_map<std::string, TypefaceFontStyleSet> registered_families_;
std::vector<std::string> family_names_;
FXL_DISALLOW_COPY_AND_ASSIGN(AssetDataProvider);
FXL_DISALLOW_COPY_AND_ASSIGN(TypefaceFontAssetProvider);
};
} // namespace txt
#endif // TXT_ASSET_DATA_PROVIDER_H_
#endif // TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_
......@@ -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 {
......
......@@ -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)
......
......@@ -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<FontCollection>()) {
font_collection_->SetAssetFontManager(sk_make_sp<AssetFontManager>(
std::make_unique<txt::DirectoryAssetDataProvider>(GetFontDir())));
}
: snapshots_(0), font_collection_(GetTestFontCollection()) {}
RenderTest::~RenderTest() = default;
......
......@@ -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 <cassert>
......
// 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 <sstream>
#include "lib/fxl/logging.h"
#include "third_party/skia/include/core/SkStream.h"
/*
* 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)
......@@ -15,8 +27,27 @@
namespace txt {
DirectoryAssetDataProvider::DirectoryAssetDataProvider(
const std::string& directory_path) {
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;
......@@ -35,7 +66,8 @@ DirectoryAssetDataProvider::DirectoryAssetDataProvider(
std::stringstream file_path;
file_path << directory_path << "/" << file_name;
RegisterTypeface(SkTypeface::MakeFromFile(file_path.str().c_str()));
font_provider.RegisterTypeface(
SkTypeface::MakeFromFile(file_path.str().c_str()));
} while (FindNextFileA(directory, &ffd) != 0);
// TODO(bkonyi): check for error here?
......@@ -65,11 +97,23 @@ DirectoryAssetDataProvider::DirectoryAssetDataProvider(
std::stringstream file_path;
file_path << directory_path << "/" << file_name;
RegisterTypeface(SkTypeface::MakeFromFile(file_path.str().c_str()));
font_provider.RegisterTypeface(
SkTypeface::MakeFromFile(file_path.str().c_str()));
}
#endif
}
DirectoryAssetDataProvider::~DirectoryAssetDataProvider() = default;
std::shared_ptr<FontCollection> GetTestFontCollection() {
std::unique_ptr<TypefaceFontAssetProvider> font_provider =
std::make_unique<TypefaceFontAssetProvider>();
RegisterFontsFromPath(*font_provider, GetFontDir());
std::shared_ptr<FontCollection> collection =
std::make_shared<FontCollection>();
collection->SetAssetFontManager(
sk_make_sp<AssetFontManager>(std::move(font_provider)));
return collection;
}
} // namespace txt
......@@ -14,15 +14,13 @@
* limitations under the License.
*/
#include <memory>
#include <string>
#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);
......
/*
* 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 <string>
#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
......@@ -146,10 +146,6 @@ FILE: ../../../flutter/shell/platform/embedder/embedder.cc
FILE: ../../../flutter/shell/platform/embedder/embedder_include.c
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.cc
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.h
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_style_set.cc
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_style_set.h
FILE: ../../../flutter/third_party/txt/src/txt/directory_asset_data_provider.cc
FILE: ../../../flutter/third_party/txt/src/txt/directory_asset_data_provider.h
FILE: ../../../flutter/third_party/txt/src/txt/platform.cc
FILE: ../../../flutter/third_party/txt/src/txt/platform.h
FILE: ../../../flutter/third_party/txt/src/txt/platform_android.cc
......@@ -493,6 +489,8 @@ FILE: ../../../flutter/flutter_kernel_transformers/lib/track_widget_constructor_
FILE: ../../../flutter/lib/ui/isolate_name_server.dart
FILE: ../../../flutter/lib/ui/painting/image_encoding.cc
FILE: ../../../flutter/lib/ui/painting/image_encoding.h
FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.cc
FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.h
FILE: ../../../flutter/shell/platform/android/apk_asset_provider.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm
......@@ -930,8 +928,6 @@ FILE: ../../../flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc
FILE: ../../../flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc
FILE: ../../../flutter/third_party/txt/benchmarks/styled_runs_benchmarks.cc
FILE: ../../../flutter/third_party/txt/benchmarks/txt_run_all_benchmarks.cc
FILE: ../../../flutter/third_party/txt/benchmarks/utils.cc
FILE: ../../../flutter/third_party/txt/benchmarks/utils.h
FILE: ../../../flutter/third_party/txt/src/log/log.cc
FILE: ../../../flutter/third_party/txt/src/log/log.h
FILE: ../../../flutter/third_party/txt/src/minikin/CmapCoverage.cpp
......@@ -970,10 +966,9 @@ FILE: ../../../flutter/third_party/txt/src/minikin/SparseBitSet.cpp
FILE: ../../../flutter/third_party/txt/src/minikin/SparseBitSet.h
FILE: ../../../flutter/third_party/txt/src/minikin/WordBreaker.cpp
FILE: ../../../flutter/third_party/txt/src/minikin/WordBreaker.h
FILE: ../../../flutter/third_party/txt/src/txt/asset_data_provider.cc
FILE: ../../../flutter/third_party/txt/src/txt/asset_data_provider.h
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_manager.cc
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_manager.h
FILE: ../../../flutter/third_party/txt/src/txt/font_asset_provider.h
FILE: ../../../flutter/third_party/txt/src/txt/font_collection.cc
FILE: ../../../flutter/third_party/txt/src/txt/font_collection.h
FILE: ../../../flutter/third_party/txt/src/txt/font_skia.cc
......@@ -997,6 +992,8 @@ FILE: ../../../flutter/third_party/txt/src/txt/text_decoration.cc
FILE: ../../../flutter/third_party/txt/src/txt/text_decoration.h
FILE: ../../../flutter/third_party/txt/src/txt/text_style.cc
FILE: ../../../flutter/third_party/txt/src/txt/text_style.h
FILE: ../../../flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc
FILE: ../../../flutter/third_party/txt/src/txt/typeface_font_asset_provider.h
FILE: ../../../flutter/third_party/txt/src/utils/JenkinsHash.cpp
FILE: ../../../flutter/third_party/txt/src/utils/JenkinsHash.h
FILE: ../../../flutter/third_party/txt/src/utils/LruCache.h
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册