1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_
6#define FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_
7
8#include <memory>
9#include <string>
10#include <unordered_map>
11#include <vector>
12
13#include "flutter/assets/asset_manager.h"
14#include "flutter/fml/macros.h"
15#include "third_party/skia/include/core/SkFontMgr.h"
16#include "third_party/skia/include/core/SkTypeface.h"
17#include "txt/font_asset_provider.h"
18
19namespace flutter {
20
21class AssetManagerFontStyleSet : public SkFontStyleSet {
22 public:
23 AssetManagerFontStyleSet(std::shared_ptr<AssetManager> asset_manager,
24 std::string family_name);
25
26 ~AssetManagerFontStyleSet() override;
27
28 void registerAsset(std::string asset);
29
30 // |SkFontStyleSet|
31 int count() override;
32
33 // |SkFontStyleSet|
34 void getStyle(int index, SkFontStyle*, SkString* style) override;
35
36 // |SkFontStyleSet|
37 SkTypeface* createTypeface(int index) override;
38
39 // |SkFontStyleSet|
40 SkTypeface* matchStyle(const SkFontStyle& pattern) override;
41
42 private:
43 std::shared_ptr<AssetManager> asset_manager_;
44 std::string family_name_;
45
46 struct TypefaceAsset {
47 TypefaceAsset(std::string a);
48
49 TypefaceAsset(const TypefaceAsset& other);
50
51 ~TypefaceAsset();
52
53 std::string asset;
54 sk_sp<SkTypeface> typeface;
55 };
56 std::vector<TypefaceAsset> assets_;
57
58 FML_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontStyleSet);
59};
60
61class AssetManagerFontProvider : public txt::FontAssetProvider {
62 public:
63 AssetManagerFontProvider(std::shared_ptr<AssetManager> asset_manager);
64
65 ~AssetManagerFontProvider() override;
66
67 void RegisterAsset(std::string family_name, std::string asset);
68
69 // |FontAssetProvider|
70 size_t GetFamilyCount() const override;
71
72 // |FontAssetProvider|
73 std::string GetFamilyName(int index) const override;
74
75 // |FontAssetProvider|
76 SkFontStyleSet* MatchFamily(const std::string& family_name) override;
77
78 private:
79 std::shared_ptr<AssetManager> asset_manager_;
80 std::unordered_map<std::string, sk_sp<AssetManagerFontStyleSet>>
81 registered_families_;
82 std::vector<std::string> family_names_;
83
84 FML_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontProvider);
85};
86
87} // namespace flutter
88
89#endif // FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_
90