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 "SkRefCnt.h"
     12 #include "SkFontStyle.h"
     13 
     14 class SkData;
     15 class SkStream;
     16 class SkString;
     17 class SkTypeface;
     18 
     19 class SK_API SkFontStyleSet : public SkRefCnt {
     20 public:
     21     SK_DECLARE_INST_COUNT(SkFontStyleSet)
     22 
     23     virtual int count() = 0;
     24     virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
     25     virtual SkTypeface* createTypeface(int index) = 0;
     26     virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
     27 
     28     static SkFontStyleSet* CreateEmpty();
     29 
     30 private:
     31     typedef SkRefCnt INHERITED;
     32 };
     33 
     34 class SkTypeface;
     35 
     36 class SK_API SkFontMgr : public SkRefCnt {
     37 public:
     38     SK_DECLARE_INST_COUNT(SkFontMgr)
     39 
     40     int countFamilies();
     41     void getFamilyName(int index, SkString* familyName);
     42     SkFontStyleSet* createStyleSet(int index);
     43 
     44     SkFontStyleSet* matchFamily(const char familyName[]);
     45 
     46     /**
     47      *  Find the closest matching typeface to the specified familyName and style
     48      *  and return a ref to it. The caller must call unref() on the returned
     49      *  object. Will never return NULL, as it will return the default font if
     50      *  no matching font is found.
     51      */
     52     SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&);
     53 
     54     SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&);
     55 
     56     /**
     57      *  Create a typeface for the specified data and TTC index (pass 0 for none)
     58      *  or NULL if the data is not recognized. The caller must call unref() on
     59      *  the returned object if it is not null.
     60      */
     61     SkTypeface* createFromData(SkData*, int ttcIndex = 0);
     62 
     63     /**
     64      *  Create a typeface for the specified stream and TTC index
     65      *  (pass 0 for none) or NULL if the stream is not recognized. The caller
     66      *  must call unref() on the returned object if it is not null.
     67      */
     68     SkTypeface* createFromStream(SkStream*, int ttcIndex = 0);
     69 
     70     /**
     71      *  Create a typeface for the specified fileName and TTC index
     72      *  (pass 0 for none) or NULL if the file is not found, or its contents are
     73      *  not recognized. The caller must call unref() on the returned object
     74      *  if it is not null.
     75      */
     76     SkTypeface* createFromFile(const char path[], int ttcIndex = 0);
     77 
     78     SkTypeface* legacyCreateTypeface(const char familyName[],
     79                                      unsigned typefaceStyleBits);
     80 
     81     /**
     82      *  Return a ref to the default fontmgr. The caller must call unref() on
     83      *  the returned object.
     84      */
     85     static SkFontMgr* RefDefault();
     86 
     87 protected:
     88     virtual int onCountFamilies() = 0;
     89     virtual void onGetFamilyName(int index, SkString* familyName) = 0;
     90     virtual SkFontStyleSet* onCreateStyleSet(int index) = 0;
     91 
     92     virtual SkFontStyleSet* onMatchFamily(const char familyName[]) = 0;
     93 
     94     virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
     95                                            const SkFontStyle&) = 0;
     96     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
     97                                          const SkFontStyle&) = 0;
     98 
     99     virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) = 0;
    100     virtual SkTypeface* onCreateFromStream(SkStream*, int ttcIndex) = 0;
    101     virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) = 0;
    102 
    103     // TODO: make this pure-virtual once all ports know about it
    104     virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
    105                                                unsigned styleBits);
    106 private:
    107     static SkFontMgr* Factory();    // implemented by porting layer
    108 
    109     typedef SkRefCnt INHERITED;
    110 };
    111 
    112 #endif
    113