Home | History | Annotate | Download | only in ports
      1 /*
      2  * Copyright 2013 Google Inc.
      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_DEFINED
      9 #define SkFontMgr_DEFINED
     10 
     11 #include "SkFontArguments.h"
     12 #include "SkFontStyle.h"
     13 #include "SkRefCnt.h"
     14 #include "SkTypes.h"
     15 
     16 class SkData;
     17 class SkFontData;
     18 class SkStreamAsset;
     19 class SkString;
     20 class SkTypeface;
     21 
     22 class SK_API SkFontStyleSet : public SkRefCnt {
     23 public:
     24     virtual int count() = 0;
     25     virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
     26     virtual SkTypeface* createTypeface(int index) = 0;
     27     virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
     28 
     29     static SkFontStyleSet* CreateEmpty();
     30 
     31 protected:
     32     SkTypeface* matchStyleCSS3(const SkFontStyle& pattern);
     33 
     34 private:
     35     typedef SkRefCnt INHERITED;
     36 };
     37 
     38 class SK_API SkFontMgr : public SkRefCnt {
     39 public:
     40     int countFamilies() const;
     41     void getFamilyName(int index, SkString* familyName) const;
     42     SkFontStyleSet* createStyleSet(int index) const;
     43 
     44     /**
     45      *  The caller must call unref() on the returned object.
     46      *  Never returns NULL; will return an empty set if the name is not found.
     47      *
     48      *  Passing nullptr as the parameter will return the default system family.
     49      *  Note that most systems don't have a default system family, so passing nullptr will often
     50      *  result in the empty set.
     51      *
     52      *  It is possible that this will return a style set not accessible from
     53      *  createStyleSet(int) due to hidden or auto-activated fonts.
     54      */
     55     SkFontStyleSet* matchFamily(const char familyName[]) const;
     56 
     57     /**
     58      *  Find the closest matching typeface to the specified familyName and style
     59      *  and return a ref to it. The caller must call unref() on the returned
     60      *  object. Will never return NULL, as it will return the default font if
     61      *  no matching font is found.
     62      *
     63      *  Passing |nullptr| as the parameter for |familyName| will return the
     64      *  default system font.
     65      *
     66      *  It is possible that this will return a style set not accessible from
     67      *  createStyleSet(int) or matchFamily(const char[]) due to hidden or
     68      *  auto-activated fonts.
     69      */
     70     SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
     71 
     72     /**
     73      *  Use the system fallback to find a typeface for the given character.
     74      *  Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
     75      *  so it is fine to just pass a ISO 639 here.
     76      *
     77      *  Will return NULL if no family can be found for the character
     78      *  in the system fallback.
     79      *
     80      *  Passing |nullptr| as the parameter for |familyName| will return the
     81      *  default system font.
     82      *
     83      *  bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
     84      *  most significant. If no specified bcp47 codes match, any font with the
     85      *  requested character will be matched.
     86      */
     87     SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
     88                                           const char* bcp47[], int bcp47Count,
     89                                           SkUnichar character) const;
     90 
     91     SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const;
     92 
     93     /**
     94      *  Create a typeface for the specified data and TTC index (pass 0 for none)
     95      *  or NULL if the data is not recognized. The caller must call unref() on
     96      *  the returned object if it is not null.
     97      */
     98     SkTypeface* createFromData(SkData*, int ttcIndex = 0) const;
     99 
    100     /**
    101      *  Create a typeface for the specified stream and TTC index
    102      *  (pass 0 for none) or NULL if the stream is not recognized. The caller
    103      *  must call unref() on the returned object if it is not null.
    104      */
    105     SkTypeface* createFromStream(SkStreamAsset*, int ttcIndex = 0) const;
    106 
    107     // deprecated, use SkFontArguments instead.
    108     using FontParameters = SkFontArguments;
    109     /* Experimental, API subject to change. */
    110     SkTypeface* createFromStream(SkStreamAsset*, const SkFontArguments&) const;
    111 
    112     /**
    113      *  Create a typeface from the specified font data.
    114      *  Will return NULL if the typeface could not be created.
    115      *  The caller must call unref() on the returned object if it is not null.
    116      */
    117     SkTypeface* createFromFontData(std::unique_ptr<SkFontData>) const;
    118 
    119     /**
    120      *  Create a typeface for the specified fileName and TTC index
    121      *  (pass 0 for none) or NULL if the file is not found, or its contents are
    122      *  not recognized. The caller must call unref() on the returned object
    123      *  if it is not null.
    124      */
    125     SkTypeface* createFromFile(const char path[], int ttcIndex = 0) const;
    126 
    127     SkTypeface* legacyCreateTypeface(const char familyName[], SkFontStyle style) const;
    128 
    129     /** Return the default fontmgr. */
    130     static sk_sp<SkFontMgr> RefDefault();
    131 
    132 protected:
    133     virtual int onCountFamilies() const = 0;
    134     virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
    135     virtual SkFontStyleSet* onCreateStyleSet(int index)const  = 0;
    136 
    137     /** May return NULL if the name is not found. */
    138     virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
    139 
    140     virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
    141                                            const SkFontStyle&) const = 0;
    142     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
    143                                                     const char* bcp47[], int bcp47Count,
    144                                                     SkUnichar character) const = 0;
    145     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
    146                                          const SkFontStyle&) const = 0;
    147 
    148     virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const = 0;
    149     virtual SkTypeface* onCreateFromStream(SkStreamAsset*, int ttcIndex) const = 0;
    150     // TODO: make pure virtual.
    151     virtual SkTypeface* onCreateFromStream(SkStreamAsset*, const SkFontArguments&) const;
    152     virtual SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData>) const;
    153     virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const = 0;
    154 
    155     virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle) const = 0;
    156 
    157 private:
    158 
    159     /** Implemented by porting layer to return the default factory. */
    160     static sk_sp<SkFontMgr> Factory();
    161 
    162     typedef SkRefCnt INHERITED;
    163 };
    164 
    165 #endif
    166