Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2011 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 
      9 
     10 #ifndef SkTypefaceCache_DEFINED
     11 #define SkTypefaceCache_DEFINED
     12 
     13 #include "SkRefCnt.h"
     14 #include "SkTypeface.h"
     15 #include "SkTArray.h"
     16 
     17 class SkTypefaceCache {
     18 public:
     19     SkTypefaceCache();
     20 
     21     /**
     22      * Callback for FindByProc. Returns true if the given typeface is a match
     23      * for the given context. The passed typeface is owned by the cache and is
     24      * not additionally ref()ed. The typeface may be in the disposed state.
     25      */
     26     typedef bool(*FindProc)(SkTypeface*, void* context);
     27 
     28     /**
     29      *  Add a typeface to the cache. This ref()s the typeface, so that the
     30      *  cache is also an owner. Later, if we need to purge the cache, typefaces
     31      *  whose refcnt is 1 (meaning only the cache is an owner) will be
     32      *  unref()ed.
     33      */
     34     void add(SkTypeface*);
     35 
     36     /**
     37      *  Iterate through the cache, calling proc(typeface, ctx) with each
     38      *  typeface. If proc returns true, then we return that typeface (this
     39      *  ref()s the typeface). If it never returns true, we return nullptr.
     40      */
     41     SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const;
     42 
     43     /**
     44      *  This will unref all of the typefaces in the cache for which the cache
     45      *  is the only owner. Normally this is handled automatically as needed.
     46      *  This function is exposed for clients that explicitly want to purge the
     47      *  cache (e.g. to look for leaks).
     48      */
     49     void purgeAll();
     50 
     51     /**
     52      *  Helper: returns a unique fontID to pass to the constructor of
     53      *  your subclass of SkTypeface
     54      */
     55     static SkFontID NewFontID();
     56 
     57     // These are static wrappers around a global instance of a cache.
     58 
     59     static void Add(SkTypeface*);
     60     static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx);
     61     static void PurgeAll();
     62 
     63     /**
     64      *  Debugging only: dumps the status of the typefaces in the cache
     65      */
     66     static void Dump();
     67 
     68 private:
     69     static SkTypefaceCache& Get();
     70 
     71     void purge(int count);
     72 
     73     SkTArray<sk_sp<SkTypeface>> fTypefaces;
     74 };
     75 
     76 #endif
     77