• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkFontMgr_custom_DEFINED
9 #define SkFontMgr_custom_DEFINED
10 
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkString.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/base/SkTArray.h"
17 #include "src/ports/SkFontHost_FreeType_common.h"
18 
19 class SkData;
20 class SkFontDescriptor;
21 class SkStreamAsset;
22 class SkTypeface;
23 
24 /** The base SkTypeface implementation for the custom font manager. */
25 class SkTypeface_Custom : public SkTypeface_FreeType {
26 public:
27     SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
28                       bool sysFont, const SkString familyName, int index);
29     bool isSysFont() const;
30 
31 protected:
32     void onGetFamilyName(SkString* familyName) const override;
33     void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
34     int getIndex() const;
35 
36 private:
37     const bool fIsSysFont;
38     const SkString fFamilyName;
39     const int fIndex;
40 
41     using INHERITED = SkTypeface_FreeType;
42 };
43 
44 /** The empty SkTypeface implementation for the custom font manager.
45  *  Used as the last resort fallback typeface.
46  */
47 class SkTypeface_Empty : public SkTypeface_Custom {
48 public:
49     SkTypeface_Empty() ;
50 
51 protected:
52     std::unique_ptr<SkStreamAsset> onOpenStream(int*) const override;
53     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
54     std::unique_ptr<SkFontData> onMakeFontData() const override;
55 
56 private:
57     using INHERITED = SkTypeface_Custom;
58 };
59 
60 /** The file SkTypeface implementation for the custom font manager. */
61 class SkTypeface_File : public SkTypeface_Custom {
62 public:
63     SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
64                     const SkString familyName, const char path[], int index);
65 
66 protected:
67     std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
68     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
69     std::unique_ptr<SkFontData> onMakeFontData() const override;
70 
71 private:
72     SkString fPath;
73 
74     using INHERITED = SkTypeface_Custom;
75 };
76 
77 ///////////////////////////////////////////////////////////////////////////////
78 
79 /**
80  *  SkFontStyleSet_Custom
81  *
82  *  This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
83  */
84 class SkFontStyleSet_Custom : public SkFontStyleSet {
85 public:
86     explicit SkFontStyleSet_Custom(const SkString familyName);
87 
88     /** Should only be called during the initial build phase. */
89     void appendTypeface(sk_sp<SkTypeface> typeface);
90     int count() override;
91     void getStyle(int index, SkFontStyle* style, SkString* name) override;
92     SkTypeface* createTypeface(int index) override;
93     SkTypeface* matchStyle(const SkFontStyle& pattern) override;
94     SkString getFamilyName();
95 
96 private:
97     SkTArray<sk_sp<SkTypeface>> fStyles;
98     SkString fFamilyName;
99 
100     friend class SkFontMgr_Custom;
101 };
102 
103 /**
104  *  SkFontMgr_Custom
105  *
106  *  This class is essentially a collection of SkFontStyleSet_Custom,
107  *  one SkFontStyleSet_Custom for each family. This class may be modified
108  *  to load fonts from any source by changing the initialization.
109  */
110 class SkFontMgr_Custom : public SkFontMgr {
111 public:
112     typedef SkTArray<sk_sp<SkFontStyleSet_Custom>> Families;
113     class SystemFontLoader {
114     public:
~SystemFontLoader()115         virtual ~SystemFontLoader() { }
116         virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
117     };
118     explicit SkFontMgr_Custom(const SystemFontLoader& loader);
119 
120 protected:
121     int onCountFamilies() const override;
122     void onGetFamilyName(int index, SkString* familyName) const override;
123     SkFontStyleSet_Custom* onCreateStyleSet(int index) const override;
124     SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override;
125     SkTypeface* onMatchFamilyStyle(const char familyName[],
126                                    const SkFontStyle& fontStyle) const override;
127     SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
128                                             const char* bcp47[], int bcp47Count,
129                                             SkUnichar character) const override;
130     sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override;
131     sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override;
132     sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override;
133     sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
134     sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override;
135 
136 private:
137     Families fFamilies;
138     SkFontStyleSet_Custom* fDefaultFamily;
139     SkTypeface_FreeType::Scanner fScanner;
140 };
141 
142 #endif
143