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