#ifndef CONTENT_RENDERER_FONT_DATA_FONT_DATA_MANAGER_H_
#define CONTENT_RENDERER_FONT_DATA_FONT_DATA_MANAGER_H_
#include <list>
#include <map>
#include <memory>
#include <string>
#include "base/containers/lru_cache.h"
#include "base/hash/hash.h"
#include "base/memory/scoped_refptr.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/sequence_local_storage_slot.h"
#include "base/unguessable_token.h"
#include "components/services/font_data/public/mojom/font_data_service.mojom.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/freetype_buildflags.h"
#include "third_party/skia/include/core/SkFontMgr.h"
#include "third_party/skia/include/core/SkFontStyle.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkTypeface.h"
namespace base {
class MemoryMappedFile;
}
namespace font_data_service {
class CONTENT_EXPORT FontDataManager : public SkFontMgr {
public:
FontDataManager();
FontDataManager(const FontDataManager&) = delete;
FontDataManager& operator=(const FontDataManager&) = delete;
~FontDataManager() override;
int onCountFamilies() const override;
void onGetFamilyName(int index, SkString* familyName) const override;
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override;
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override;
sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[],
const SkFontStyle&) const override;
sk_sp<SkTypeface> onMatchFamilyStyleCharacter(
const char familyName[],
const SkFontStyle&,
const char* bcp47[],
int bcp47Count,
SkUnichar character) const override;
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override;
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
int ttcIndex) const override;
sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
const SkFontArguments&) const override;
sk_sp<SkTypeface> onMakeFromFile(const char path[],
int ttcIndex) const override;
sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
SkFontStyle) const override;
void SetFontServiceForTesting(
mojo::PendingRemote<font_data_service::mojom::FontDataService>
font_data_service);
private:
font_data_service::mojom::FontDataService& GetRemoteFontDataService() const;
sk_sp<SkTypeface> CreateTypefaceFromMatchResult(
mojom::MatchFamilyNameResultPtr match_result) const;
void GetAllFamilyNames() const;
struct MatchFamilyRequest {
std::string name;
int weight;
int width;
SkFontStyle::Slant slant;
};
struct MatchFamilyRequestHash {
size_t operator()(const MatchFamilyRequest& key) const {
return base::HashCombine(0ull, key.name, key.weight, key.width,
key.slant);
}
};
struct MatchFamilyRequestEqual {
size_t operator()(const MatchFamilyRequest& lhs,
const MatchFamilyRequest& rhs) const {
return lhs.name == rhs.name && lhs.weight == rhs.weight &&
lhs.width == rhs.width && lhs.slant == rhs.slant;
}
};
mutable base::HashingLRUCache<MatchFamilyRequest,
sk_sp<SkTypeface>,
MatchFamilyRequestHash,
MatchFamilyRequestEqual>
typeface_cache_;
mutable base::Lock lock_;
mutable std::map<base::UnguessableToken, base::ReadOnlySharedMemoryMapping>
mapped_regions_;
mutable std::list<std::unique_ptr<base::MemoryMappedFile>> mapped_files_;
mutable std::vector<std::string> family_names_;
#if BUILDFLAG(ENABLE_FREETYPE)
sk_sp<SkFontMgr> custom_fnt_mgr_;
#endif
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
mutable base::SequenceLocalStorageSlot<
mojo::Remote<font_data_service::mojom::FontDataService>>
font_data_service_slot_;
};
}
#endif