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/SkTArray.h"
17#include "src/ports/SkFontHost_FreeType_common.h"
18
19class SkData;
20class SkFontDescriptor;
21class SkStreamAsset;
22class SkTypeface;
23
24/** The base SkTypeface implementation for the custom font manager. */
25class SkTypeface_Custom : public SkTypeface_FreeType {
26public:
27 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
28 bool sysFont, const SkString familyName, int index);
29 bool isSysFont() const;
30
31protected:
32 void onGetFamilyName(SkString* familyName) const override;
33 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
34 int getIndex() const;
35
36private:
37 const bool fIsSysFont;
38 const SkString fFamilyName;
39 const int fIndex;
40
41 typedef SkTypeface_FreeType INHERITED;
42};
43
44/** The empty SkTypeface implementation for the custom font manager.
45 * Used as the last resort fallback typeface.
46 */
47class SkTypeface_Empty : public SkTypeface_Custom {
48public:
49 SkTypeface_Empty() ;
50
51protected:
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
56private:
57 typedef SkTypeface_Custom INHERITED;
58};
59
60/** The stream SkTypeface implementation for the custom font manager. */
61class SkTypeface_Stream : public SkTypeface_Custom {
62public:
63 SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
64 const SkFontStyle& style, bool isFixedPitch, bool sysFont,
65 const SkString familyName);
66
67protected:
68 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
69 std::unique_ptr<SkFontData> onMakeFontData() const override;
70 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
71
72private:
73 const std::unique_ptr<const SkFontData> fData;
74
75 typedef SkTypeface_Custom INHERITED;
76};
77
78/** The file SkTypeface implementation for the custom font manager. */
79class SkTypeface_File : public SkTypeface_Custom {
80public:
81 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
82 const SkString familyName, const char path[], int index);
83
84protected:
85 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
86 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
87 std::unique_ptr<SkFontData> onMakeFontData() const override;
88
89private:
90 SkString fPath;
91
92 typedef SkTypeface_Custom INHERITED;
93};
94
95///////////////////////////////////////////////////////////////////////////////
96
97/**
98 * SkFontStyleSet_Custom
99 *
100 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
101 */
102class SkFontStyleSet_Custom : public SkFontStyleSet {
103public:
104 explicit SkFontStyleSet_Custom(const SkString familyName);
105
106 /** Should only be called during the initial build phase. */
107 void appendTypeface(sk_sp<SkTypeface_Custom> typeface);
108 int count() override;
109 void getStyle(int index, SkFontStyle* style, SkString* name) override;
110 SkTypeface* createTypeface(int index) override;
111 SkTypeface* matchStyle(const SkFontStyle& pattern) override;
112 SkString getFamilyName();
113
114private:
115 SkTArray<sk_sp<SkTypeface_Custom>> fStyles;
116 SkString fFamilyName;
117
118 friend class SkFontMgr_Custom;
119};
120
121/**
122 * SkFontMgr_Custom
123 *
124 * This class is essentially a collection of SkFontStyleSet_Custom,
125 * one SkFontStyleSet_Custom for each family. This class may be modified
126 * to load fonts from any source by changing the initialization.
127 */
128class SkFontMgr_Custom : public SkFontMgr {
129public:
130 typedef SkTArray<sk_sp<SkFontStyleSet_Custom>> Families;
131 class SystemFontLoader {
132 public:
133 virtual ~SystemFontLoader() { }
134 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
135 };
136 explicit SkFontMgr_Custom(const SystemFontLoader& loader);
137
138protected:
139 int onCountFamilies() const override;
140 void onGetFamilyName(int index, SkString* familyName) const override;
141 SkFontStyleSet_Custom* onCreateStyleSet(int index) const override;
142 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override;
143 SkTypeface* onMatchFamilyStyle(const char familyName[],
144 const SkFontStyle& fontStyle) const override;
145 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
146 const char* bcp47[], int bcp47Count,
147 SkUnichar character) const override;
148 SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
149 const SkFontStyle& fontStyle) const override;
150 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override;
151 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override;
152 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override;
153 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> data) const override;
154 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
155 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override;
156
157private:
158 Families fFamilies;
159 SkFontStyleSet_Custom* fDefaultFamily;
160 SkTypeface_FreeType::Scanner fScanner;
161};
162
163#endif
164