epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 11 | #ifndef SkTypefaceCache_DEFINED |
| 12 | #define SkTypefaceCache_DEFINED |
| 13 | |
| 14 | #include "SkTypeface.h" |
| 15 | #include "SkTDArray.h" |
| 16 | |
| 17 | /* TODO |
| 18 | * Provide std way to cache name+requestedStyle aliases to the same typeface. |
| 19 | * |
| 20 | * The current mechanism ends up create a diff typeface for each one, even if |
| 21 | * they map to the same internal obj (e.g. CTFontRef on the mac) |
| 22 | */ |
| 23 | |
| 24 | class SkTypefaceCache { |
| 25 | public: |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 26 | SkTypefaceCache(); |
| 27 | ~SkTypefaceCache(); |
| 28 | |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Callback for FindByProc. Returns true if the given typeface is a match |
| 31 | * for the given context. The passed typeface is owned by the cache and is |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 32 | * not additionally ref()ed. The typeface may be in the disposed state. |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 33 | */ |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 34 | typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context); |
| 35 | |
| 36 | /** |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 37 | * Add a typeface to the cache. This ref()s the typeface, so that the |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 38 | * cache is also an owner. Later, if we need to purge the cache, typefaces |
| 39 | * whose refcnt is 1 (meaning only the cache is an owner) will be |
| 40 | * unref()ed. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 41 | */ |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 42 | void add(SkTypeface*, SkTypeface::Style requested, bool strong = true); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Search the cache for a typeface with the specified fontID (uniqueID). |
| 46 | * If one is found, return it (its reference count is unmodified). If none |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 47 | * is found, return NULL. The reference count is unmodified as it is |
| 48 | * assumed that the stack will contain a ref to the typeface. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 49 | */ |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 50 | SkTypeface* findByID(SkFontID findID) const; |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Iterate through the cache, calling proc(typeface, ctx) with each |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 54 | * typeface. If proc returns true, then we return that typeface (this |
| 55 | * ref()s the typeface). If it never returns true, we return NULL. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 56 | */ |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 57 | SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const; |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 60 | * This will unref all of the typefaces in the cache for which the cache |
| 61 | * is the only owner. Normally this is handled automatically as needed. |
| 62 | * This function is exposed for clients that explicitly want to purge the |
| 63 | * cache (e.g. to look for leaks). |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 64 | */ |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 65 | void purgeAll(); |
| 66 | |
| 67 | /** |
| 68 | * Helper: returns a unique fontID to pass to the constructor of |
| 69 | * your subclass of SkTypeface |
| 70 | */ |
| 71 | static SkFontID NewFontID(); |
| 72 | |
| 73 | // These are static wrappers around a global instance of a cache. |
| 74 | |
| 75 | static void Add(SkTypeface*, |
| 76 | SkTypeface::Style requested, |
| 77 | bool strong = true); |
| 78 | static SkTypeface* FindByID(SkFontID fontID); |
| 79 | static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx); |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 80 | static void PurgeAll(); |
| 81 | |
| 82 | /** |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 83 | * Debugging only: dumps the status of the typefaces in the cache |
| 84 | */ |
| 85 | static void Dump(); |
| 86 | |
| 87 | private: |
| 88 | static SkTypefaceCache& Get(); |
| 89 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 90 | void purge(int count); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 91 | |
| 92 | struct Rec { |
| 93 | SkTypeface* fFace; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 94 | bool fStrong; |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 95 | SkTypeface::Style fRequestedStyle; |
| 96 | }; |
| 97 | SkTDArray<Rec> fArray; |
| 98 | }; |
| 99 | |
| 100 | #endif |