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 SkFontConfigInterface_DEFINED
      9 #define SkFontConfigInterface_DEFINED
     10 
     11 #include "SkDataTable.h"
     12 #include "SkFontStyle.h"
     13 #include "SkRefCnt.h"
     14 #include "SkTypeface.h"
     15 
     16 class SkFontMgr;
     17 
     18 /**
     19  *  \class SkFontConfigInterface
     20  *
     21  *  A simple interface for remotable font management.
     22  *  The global instance can be found with RefGlobal().
     23  */
     24 class SK_API SkFontConfigInterface : public SkRefCnt {
     25 public:
     26 
     27     /**
     28      *  Returns the global SkFontConfigInterface instance. If it is not
     29      *  nullptr, calls ref() on it. The caller must balance this with a call to
     30      *  unref(). The default SkFontConfigInterface is the result of calling
     31      *  GetSingletonDirectInterface.
     32      */
     33     static SkFontConfigInterface* RefGlobal();
     34 
     35     /**
     36      *  Replace the current global instance with the specified one, safely
     37      *  ref'ing the new instance, and unref'ing the previous. Returns its
     38      *  parameter (the new global instance).
     39      */
     40     static SkFontConfigInterface* SetGlobal(SkFontConfigInterface*);
     41 
     42     /**
     43      *  This should be treated as private to the impl of SkFontConfigInterface.
     44      *  Callers should not change or expect any particular values. It is meant
     45      *  to be a union of possible storage types to aid the impl.
     46      */
     47     struct FontIdentity {
     48         FontIdentity() : fID(0), fTTCIndex(0) {}
     49 
     50         bool operator==(const FontIdentity& other) const {
     51             return fID == other.fID &&
     52                    fTTCIndex == other.fTTCIndex &&
     53                    fString == other.fString;
     54         }
     55         bool operator!=(const FontIdentity& other) const {
     56             return !(*this == other);
     57         }
     58 
     59         uint32_t    fID;
     60         int32_t     fTTCIndex;
     61         SkString    fString;
     62         SkFontStyle fStyle;
     63 
     64         // If buffer is NULL, just return the number of bytes that would have
     65         // been written. Will pad contents to a multiple of 4.
     66         size_t writeToMemory(void* buffer = NULL) const;
     67 
     68         // Recreate from a flattened buffer, returning the number of bytes read.
     69         size_t readFromMemory(const void* buffer, size_t length);
     70     };
     71 
     72     /**
     73      *  Given a familyName and style, find the best match.
     74      *
     75      *  If a match is found, return true and set its outFontIdentifier.
     76      *      If outFamilyName is not null, assign the found familyName to it
     77      *          (which may differ from the requested familyName).
     78      *      If outStyle is not null, assign the found style to it
     79      *          (which may differ from the requested style).
     80      *
     81      *  If a match is not found, return false, and ignore all out parameters.
     82      */
     83     virtual bool matchFamilyName(const char familyName[],
     84                                  SkFontStyle requested,
     85                                  FontIdentity* outFontIdentifier,
     86                                  SkString* outFamilyName,
     87                                  SkFontStyle* outStyle) = 0;
     88 
     89     /**
     90      *  Given a FontRef, open a stream to access its data, or return null
     91      *  if the FontRef's data is not available. The caller is responsible for
     92      *  deleting the stream when it is done accessing the data.
     93      */
     94     virtual SkStreamAsset* openStream(const FontIdentity&) = 0;
     95 
     96     /**
     97      *  Return an SkTypeface for the given FontIdentity.
     98      *
     99      *  The default implementation simply returns a new typeface built using data obtained from
    100      *  openStream(), but derived classes may implement more complex caching schemes.
    101      */
    102     virtual sk_sp<SkTypeface> makeTypeface(const FontIdentity& identity) {
    103         return SkTypeface::MakeFromStream(this->openStream(identity), identity.fTTCIndex);
    104     }
    105 
    106     /**
    107      *  Return a singleton instance of a direct subclass that calls into
    108      *  libfontconfig. This does not affect the refcnt of the returned instance.
    109      */
    110     static SkFontConfigInterface* GetSingletonDirectInterface();
    111 
    112     typedef SkRefCnt INHERITED;
    113 };
    114 
    115 #endif
    116