blob: 02bbf45c5a26da55048aab628e07591a09f5315b [file] [log] [blame]
Ben Wagner8ab590f2017-02-08 17:29:33 -05001/*
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 "SkFontHost_FreeType_common.h"
12#include "SkFontMgr.h"
13#include "SkFontStyle.h"
14#include "SkRefCnt.h"
15#include "SkString.h"
16#include "SkTArray.h"
17#include "SkTypes.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 SkStreamAsset* onOpenStream(int*) const override;
Bruce Wang0ea256c2018-06-22 15:44:47 -040053 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
Ben Wagner8ab590f2017-02-08 17:29:33 -050054
55private:
56 typedef SkTypeface_Custom INHERITED;
57};
58
59/** The stream SkTypeface implementation for the custom font manager. */
60class SkTypeface_Stream : public SkTypeface_Custom {
61public:
62 SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
63 const SkFontStyle& style, bool isFixedPitch, bool sysFont,
64 const SkString familyName);
65
66protected:
67 SkStreamAsset* onOpenStream(int* ttcIndex) const override;
68 std::unique_ptr<SkFontData> onMakeFontData() const override;
Bruce Wang0ea256c2018-06-22 15:44:47 -040069 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
Ben Wagner8ab590f2017-02-08 17:29:33 -050070
71private:
72 const std::unique_ptr<const SkFontData> fData;
73
74 typedef SkTypeface_Custom INHERITED;
75};
76
77/** The file SkTypeface implementation for the custom font manager. */
78class SkTypeface_File : public SkTypeface_Custom {
79public:
80 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
81 const SkString familyName, const char path[], int index);
82
83protected:
84 SkStreamAsset* onOpenStream(int* ttcIndex) const override;
Bruce Wang0ea256c2018-06-22 15:44:47 -040085 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
Ben Wagner8ab590f2017-02-08 17:29:33 -050086
87private:
88 SkString fPath;
89
90 typedef SkTypeface_Custom INHERITED;
91};
92
93///////////////////////////////////////////////////////////////////////////////
94
95/**
96 * SkFontStyleSet_Custom
97 *
98 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
99 */
100class SkFontStyleSet_Custom : public SkFontStyleSet {
101public:
102 explicit SkFontStyleSet_Custom(const SkString familyName);
103
104 /** Should only be called during the inital build phase. */
105 void appendTypeface(sk_sp<SkTypeface_Custom> typeface);
106 int count() override;
107 void getStyle(int index, SkFontStyle* style, SkString* name) override;
108 SkTypeface* createTypeface(int index) override;
109 SkTypeface* matchStyle(const SkFontStyle& pattern) override;
110 SkString getFamilyName();
111
112private:
113 SkTArray<sk_sp<SkTypeface_Custom>> fStyles;
114 SkString fFamilyName;
115
116 friend class SkFontMgr_Custom;
117};
118
119/**
120 * SkFontMgr_Custom
121 *
122 * This class is essentially a collection of SkFontStyleSet_Custom,
123 * one SkFontStyleSet_Custom for each family. This class may be modified
124 * to load fonts from any source by changing the initialization.
125 */
126class SkFontMgr_Custom : public SkFontMgr {
127public:
128 typedef SkTArray<sk_sp<SkFontStyleSet_Custom>> Families;
129 class SystemFontLoader {
130 public:
131 virtual ~SystemFontLoader() { }
132 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
133 };
134 explicit SkFontMgr_Custom(const SystemFontLoader& loader);
135
136protected:
137 int onCountFamilies() const override;
138 void onGetFamilyName(int index, SkString* familyName) const override;
139 SkFontStyleSet_Custom* onCreateStyleSet(int index) const override;
140 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override;
141 SkTypeface* onMatchFamilyStyle(const char familyName[],
142 const SkFontStyle& fontStyle) const override;
143 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
144 const char* bcp47[], int bcp47Count,
145 SkUnichar character) const override;
146 SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
147 const SkFontStyle& fontStyle) const override;
Mike Reed59227392017-09-26 09:46:08 -0400148 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override;
149 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override;
150 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override;
151 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> data) const override;
152 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
153 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override;
Ben Wagner8ab590f2017-02-08 17:29:33 -0500154
155private:
156 Families fFamilies;
157 SkFontStyleSet_Custom* fDefaultFamily;
158 SkTypeface_FreeType::Scanner fScanner;
159};
160
161#endif