Select fonts from asset directory instead of FLX (flutter/engine#4464)
This commit is contained in:
@@ -9,10 +9,12 @@
|
||||
#include <vector>
|
||||
|
||||
#include "lib/fxl/macros.h"
|
||||
#include "lib/fxl/memory/ref_counted.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class DirectoryAssetBundle {
|
||||
class DirectoryAssetBundle
|
||||
: public fxl::RefCountedThreadSafe<DirectoryAssetBundle> {
|
||||
public:
|
||||
explicit DirectoryAssetBundle(std::string directory);
|
||||
~DirectoryAssetBundle();
|
||||
|
||||
@@ -34,14 +34,15 @@ std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const {
|
||||
return collection_;
|
||||
}
|
||||
|
||||
void FontCollection::RegisterFontsFromAssetStore(
|
||||
fxl::RefPtr<blink::ZipAssetStore> asset_store) {
|
||||
if (!asset_store) {
|
||||
void FontCollection::RegisterFontsFromDirectoryAssetBundle(
|
||||
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle) {
|
||||
if (!directory_asset_bundle) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> manifest_data;
|
||||
if (!asset_store->GetAsBuffer("FontManifest.json", &manifest_data)) {
|
||||
if (!directory_asset_bundle->GetAsBuffer("FontManifest.json",
|
||||
&manifest_data)) {
|
||||
FXL_DLOG(WARNING) << "Could not find the font manifest in the asset store.";
|
||||
return;
|
||||
}
|
||||
@@ -91,7 +92,8 @@ void FontCollection::RegisterFontsFromAssetStore(
|
||||
|
||||
// TODO: Handle weights and styles.
|
||||
std::vector<uint8_t> font_data;
|
||||
if (asset_store->GetAsBuffer(font_asset->value.GetString(), &font_data)) {
|
||||
if (directory_asset_bundle->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 =
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "flutter/assets/zip_asset_store.h"
|
||||
#include "flutter/assets/directory_asset_bundle.h"
|
||||
#include "lib/fxl/macros.h"
|
||||
#include "lib/fxl/memory/ref_ptr.h"
|
||||
#include "txt/asset_data_provider.h"
|
||||
@@ -21,8 +21,8 @@ class FontCollection {
|
||||
|
||||
std::shared_ptr<txt::FontCollection> GetFontCollection() const;
|
||||
|
||||
void RegisterFontsFromAssetStore(
|
||||
fxl::RefPtr<blink::ZipAssetStore> asset_store);
|
||||
void RegisterFontsFromDirectoryAssetBundle(
|
||||
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle);
|
||||
|
||||
void RegisterTestFonts();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include "flutter/runtime/asset_font_selector.h"
|
||||
|
||||
#include "flutter/assets/zip_asset_store.h"
|
||||
#include "flutter/assets/directory_asset_bundle.h"
|
||||
#include "flutter/lib/ui/ui_dart_state.h"
|
||||
#include "flutter/sky/engine/platform/fonts/FontData.h"
|
||||
#include "flutter/sky/engine/platform/fonts/FontFaceCreationParams.h"
|
||||
@@ -80,15 +80,17 @@ struct FontMatcher {
|
||||
|
||||
} // namespace
|
||||
|
||||
void AssetFontSelector::Install(fxl::RefPtr<ZipAssetStore> asset_store) {
|
||||
void AssetFontSelector::Install(
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle) {
|
||||
RefPtr<AssetFontSelector> font_selector =
|
||||
adoptRef(new AssetFontSelector(std::move(asset_store)));
|
||||
adoptRef(new AssetFontSelector(std::move(directory_asset_bundle)));
|
||||
font_selector->parseFontManifest();
|
||||
UIDartState::Current()->set_font_selector(font_selector);
|
||||
}
|
||||
|
||||
AssetFontSelector::AssetFontSelector(fxl::RefPtr<ZipAssetStore> asset_store)
|
||||
: asset_store_(std::move(asset_store)) {}
|
||||
AssetFontSelector::AssetFontSelector(
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle)
|
||||
: directory_asset_bundle_(std::move(directory_asset_bundle)) {}
|
||||
|
||||
AssetFontSelector::~AssetFontSelector() {}
|
||||
|
||||
@@ -106,7 +108,8 @@ AssetFontSelector::FlutterFontAttributes::~FlutterFontAttributes() {}
|
||||
|
||||
void AssetFontSelector::parseFontManifest() {
|
||||
std::vector<uint8_t> font_manifest_data;
|
||||
if (!asset_store_->GetAsBuffer(kFontManifestAssetPath, &font_manifest_data))
|
||||
if (!directory_asset_bundle_->GetAsBuffer(kFontManifestAssetPath,
|
||||
&font_manifest_data))
|
||||
return;
|
||||
|
||||
rapidjson::Document document;
|
||||
@@ -222,7 +225,8 @@ sk_sp<SkTypeface> AssetFontSelector::getTypefaceAsset(
|
||||
}
|
||||
|
||||
std::unique_ptr<TypefaceAsset> typeface_asset(new TypefaceAsset);
|
||||
if (!asset_store_->GetAsBuffer(asset_path, &typeface_asset->data)) {
|
||||
if (!directory_asset_bundle_->GetAsBuffer(asset_path,
|
||||
&typeface_asset->data)) {
|
||||
typeface_cache_.insert(std::make_pair(asset_path, nullptr));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/assets/zip_asset_store.h"
|
||||
#include "flutter/assets/directory_asset_bundle.h"
|
||||
#include "flutter/sky/engine/platform/fonts/FontCacheKey.h"
|
||||
#include "flutter/sky/engine/platform/fonts/FontSelector.h"
|
||||
#include "flutter/sky/engine/platform/fonts/SimpleFontData.h"
|
||||
@@ -16,14 +16,14 @@
|
||||
namespace blink {
|
||||
|
||||
// A FontSelector implementation that resolves custon font names to assets
|
||||
// loaded from the FLX.
|
||||
// loaded from the asset directory.
|
||||
class AssetFontSelector : public FontSelector {
|
||||
public:
|
||||
struct FlutterFontAttributes;
|
||||
|
||||
~AssetFontSelector() override;
|
||||
|
||||
static void Install(fxl::RefPtr<ZipAssetStore> asset_store);
|
||||
static void Install(fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle);
|
||||
|
||||
PassRefPtr<FontData> getFontData(const FontDescription& font_description,
|
||||
const AtomicString& family_name) override;
|
||||
@@ -39,14 +39,15 @@ class AssetFontSelector : public FontSelector {
|
||||
private:
|
||||
struct TypefaceAsset;
|
||||
|
||||
explicit AssetFontSelector(fxl::RefPtr<ZipAssetStore> asset_store);
|
||||
explicit AssetFontSelector(
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle);
|
||||
|
||||
void parseFontManifest();
|
||||
|
||||
sk_sp<SkTypeface> getTypefaceAsset(const FontDescription& font_description,
|
||||
const AtomicString& family_name);
|
||||
|
||||
fxl::RefPtr<ZipAssetStore> asset_store_;
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle_;
|
||||
|
||||
HashMap<AtomicString, std::vector<FlutterFontAttributes>> font_family_map_;
|
||||
|
||||
|
||||
@@ -578,8 +578,7 @@ void Engine::SetSemanticsEnabled(bool enabled) {
|
||||
void Engine::ConfigureAssetBundle(const std::string& path) {
|
||||
struct stat stat_result = {};
|
||||
|
||||
directory_asset_bundle_.reset();
|
||||
// TODO(abarth): We should reset asset_store_ as well, but that might break
|
||||
// TODO(abarth): We should reset directory_asset_bundle_, but that might break
|
||||
// custom font loading in hot reload.
|
||||
|
||||
if (::stat(path.c_str(), &stat_result) != 0) {
|
||||
@@ -590,7 +589,7 @@ void Engine::ConfigureAssetBundle(const std::string& path) {
|
||||
std::string flx_path;
|
||||
if (S_ISDIR(stat_result.st_mode)) {
|
||||
directory_asset_bundle_ =
|
||||
std::make_unique<blink::DirectoryAssetBundle>(path);
|
||||
fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path);
|
||||
flx_path = files::GetDirectoryName(path) + "/app.flx";
|
||||
} else if (S_ISREG(stat_result.st_mode)) {
|
||||
flx_path = path;
|
||||
@@ -622,11 +621,11 @@ void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
|
||||
blink::TestFontSelector::Install();
|
||||
if (!blink::Settings::Get().using_blink)
|
||||
blink::FontCollection::ForProcess().RegisterTestFonts();
|
||||
} else if (asset_store_) {
|
||||
blink::AssetFontSelector::Install(asset_store_);
|
||||
} else if (directory_asset_bundle_) {
|
||||
blink::AssetFontSelector::Install(directory_asset_bundle_);
|
||||
if (!blink::Settings::Get().using_blink) {
|
||||
blink::FontCollection::ForProcess().RegisterFontsFromAssetStore(
|
||||
asset_store_);
|
||||
blink::FontCollection::ForProcess().RegisterFontsFromDirectoryAssetBundle(
|
||||
directory_asset_bundle_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ class Engine : public blink::RuntimeDelegate {
|
||||
bool semantics_enabled_ = false;
|
||||
// TODO(zarah): Remove usage of asset_store_ once app.flx is removed.
|
||||
fxl::RefPtr<blink::ZipAssetStore> asset_store_;
|
||||
std::unique_ptr<blink::DirectoryAssetBundle> directory_asset_bundle_;
|
||||
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle_;
|
||||
// TODO(eseidel): This should move into an AnimatorStateMachine.
|
||||
bool activity_running_;
|
||||
bool have_surface_;
|
||||
|
||||
Reference in New Issue
Block a user