1/*
2 * Copyright 2017 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef TXT_ASSET_FONT_MANAGER_H_
18#define TXT_ASSET_FONT_MANAGER_H_
19
20#include <memory>
21#include "flutter/fml/macros.h"
22#include "third_party/skia/include/core/SkFontMgr.h"
23#include "third_party/skia/include/core/SkStream.h"
24#include "txt/font_asset_provider.h"
25#include "txt/typeface_font_asset_provider.h"
26
27namespace txt {
28
29class AssetFontManager : public SkFontMgr {
30 public:
31 AssetFontManager(std::unique_ptr<FontAssetProvider> font_provider);
32
33 ~AssetFontManager() override;
34
35 protected:
36 // |SkFontMgr|
37 SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
38
39 std::unique_ptr<FontAssetProvider> font_provider_;
40
41 private:
42 // |SkFontMgr|
43 int onCountFamilies() const override;
44
45 // |SkFontMgr|
46 void onGetFamilyName(int index, SkString* familyName) const override;
47
48 // |SkFontMgr|
49 SkFontStyleSet* onCreateStyleSet(int index) const override;
50
51 // |SkFontMgr|
52 SkTypeface* onMatchFamilyStyle(const char familyName[],
53 const SkFontStyle&) const override;
54
55 // |SkFontMgr|
56 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
57 const SkFontStyle&,
58 const char* bcp47[],
59 int bcp47Count,
60 SkUnichar character) const override;
61
62 // |SkFontMgr|
63 SkTypeface* onMatchFaceStyle(const SkTypeface*,
64 const SkFontStyle&) const override;
65
66 // |SkFontMgr|
67 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override;
68
69 // |SkFontMgr|
70 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
71 int ttcIndex) const override;
72
73 // |SkFontMgr|
74 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
75 const SkFontArguments&) const override;
76
77 // |SkFontMgr|
78 sk_sp<SkTypeface> onMakeFromFile(const char path[],
79 int ttcIndex) const override;
80
81 // |SkFontMgr|
82 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
83 SkFontStyle) const override;
84
85 FML_DISALLOW_COPY_AND_ASSIGN(AssetFontManager);
86};
87
88class DynamicFontManager : public AssetFontManager {
89 public:
90 DynamicFontManager()
91 : AssetFontManager(std::make_unique<TypefaceFontAssetProvider>()) {}
92
93 TypefaceFontAssetProvider& font_provider() {
94 return static_cast<TypefaceFontAssetProvider&>(*font_provider_);
95 }
96};
97
98} // namespace txt
99
100#endif // TXT_ASSET_FONT_MANAGER_H_
101