1 | /* |
---|---|
2 | * Copyright 2018 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_TYPEFACE_FONT_ASSET_PROVIDER_H_ |
18 | #define TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_ |
19 | |
20 | #include <string> |
21 | #include <unordered_map> |
22 | #include <vector> |
23 | |
24 | #include "flutter/fml/macros.h" |
25 | #include "third_party/skia/include/core/SkFontMgr.h" |
26 | #include "txt/font_asset_provider.h" |
27 | |
28 | namespace txt { |
29 | |
30 | class TypefaceFontStyleSet : public SkFontStyleSet { |
31 | public: |
32 | TypefaceFontStyleSet(); |
33 | |
34 | ~TypefaceFontStyleSet() override; |
35 | |
36 | void registerTypeface(sk_sp<SkTypeface> typeface); |
37 | |
38 | // |SkFontStyleSet| |
39 | int count() override; |
40 | |
41 | // |SkFontStyleSet| |
42 | void getStyle(int index, SkFontStyle* style, SkString* name) override; |
43 | |
44 | // |SkFontStyleSet| |
45 | SkTypeface* createTypeface(int index) override; |
46 | |
47 | // |SkFontStyleSet| |
48 | SkTypeface* matchStyle(const SkFontStyle& pattern) override; |
49 | |
50 | private: |
51 | std::vector<sk_sp<SkTypeface>> typefaces_; |
52 | |
53 | FML_DISALLOW_COPY_AND_ASSIGN(TypefaceFontStyleSet); |
54 | }; |
55 | |
56 | class TypefaceFontAssetProvider : public FontAssetProvider { |
57 | public: |
58 | TypefaceFontAssetProvider(); |
59 | ~TypefaceFontAssetProvider() override; |
60 | |
61 | void RegisterTypeface(sk_sp<SkTypeface> typeface); |
62 | |
63 | void RegisterTypeface(sk_sp<SkTypeface> typeface, |
64 | std::string family_name_alias); |
65 | |
66 | // |FontAssetProvider| |
67 | size_t GetFamilyCount() const override; |
68 | |
69 | // |FontAssetProvider| |
70 | std::string GetFamilyName(int index) const override; |
71 | |
72 | // |FontAssetProvider| |
73 | SkFontStyleSet* MatchFamily(const std::string& family_name) override; |
74 | |
75 | private: |
76 | std::unordered_map<std::string, sk_sp<TypefaceFontStyleSet>> |
77 | registered_families_; |
78 | std::vector<std::string> family_names_; |
79 | |
80 | FML_DISALLOW_COPY_AND_ASSIGN(TypefaceFontAssetProvider); |
81 | }; |
82 | |
83 | } // namespace txt |
84 | |
85 | #endif // TXT_TYPEFACE_FONT_ASSET_PROVIDER_H_ |
86 |