Home | History | Annotate | Download | only in ports
      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 
     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     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  */
     47 class SkTypeface_Empty : public SkTypeface_Custom {
     48 public:
     49     SkTypeface_Empty() ;
     50 
     51 protected:
     52     SkStreamAsset* onOpenStream(int*) const override;
     53     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
     54 
     55 private:
     56     typedef SkTypeface_Custom INHERITED;
     57 };
     58 
     59 /** The stream SkTypeface implementation for the custom font manager. */
     60 class SkTypeface_Stream : public SkTypeface_Custom {
     61 public:
     62     SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
     63                       const SkFontStyle& style, bool isFixedPitch, bool sysFont,
     64                       const SkString familyName);
     65 
     66 protected:
     67     SkStreamAsset* onOpenStream(int* ttcIndex) const override;
     68     std::unique_ptr<SkFontData> onMakeFontData() const override;
     69     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
     70 
     71 private:
     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. */
     78 class SkTypeface_File : public SkTypeface_Custom {
     79 public:
     80     SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
     81                     const SkString familyName, const char path[], int index);
     82 
     83 protected:
     84     SkStreamAsset* onOpenStream(int* ttcIndex) const override;
     85     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override;
     86 
     87 private:
     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  */
    100 class SkFontStyleSet_Custom : public SkFontStyleSet {
    101 public:
    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 
    112 private:
    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  */
    126 class SkFontMgr_Custom : public SkFontMgr {
    127 public:
    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 
    136 protected:
    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;
    148     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;
    154 
    155 private:
    156     Families fFamilies;
    157     SkFontStyleSet_Custom* fDefaultFamily;
    158     SkTypeface_FreeType::Scanner fScanner;
    159 };
    160 
    161 #endif
    162