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.
This commit is contained in:
Jason Simmons
2018-06-14 16:13:56 -07:00
committed by GitHub
parent 3a6e1d0e43
commit 97cf68d6c8
33 changed files with 603 additions and 487 deletions

View File

@@ -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",

View File

@@ -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<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

View File

@@ -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 <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_

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -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;

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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 <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

View File

@@ -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<SkTypeface> 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<SkTypeface> 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

View File

@@ -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 <string>
#include <unordered_map>
#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<SkTypeface> typeface);
void RegisterTypeface(sk_sp<SkTypeface> typeface,
std::string family_name_alias);
private:
std::unordered_map<std::string, AssetFontStyleSet> registered_families_;
std::vector<std::string> family_names_;
FXL_DISALLOW_COPY_AND_ASSIGN(AssetDataProvider);
};
} // namespace txt
#endif // TXT_ASSET_DATA_PROVIDER_H_

View File

@@ -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);

View File

@@ -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;

View File

@@ -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<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

View File

@@ -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 <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_

View File

@@ -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 <sstream>
#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 <dirent.h>
#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<DIR, decltype(directory_closer)> 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

View File

@@ -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_

View File

@@ -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 <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_

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<SkTypeface> 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<SkTypeface> 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<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

View File

@@ -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 <string>
#include <unordered_map>
#include <vector>
#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<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(TypefaceFontStyleSet);
};
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, TypefaceFontStyleSet> registered_families_;
std::vector<std::string> family_names_;
FXL_DISALLOW_COPY_AND_ASSIGN(TypefaceFontAssetProvider);
};
} // namespace txt
#endif // TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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;

View File

@@ -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>

View File

@@ -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 <dirent.h>
#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<DIR, decltype(directory_closer)> 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<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

View File

@@ -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);

View File

@@ -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 <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