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: |
| 26 | typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context); |
| 27 | |
| 28 | /** |
| 29 | * Helper: returns a unique fontID to pass to the constructor of |
| 30 | * your subclass of SkTypeface |
| 31 | */ |
| 32 | static SkFontID NewFontID(); |
| 33 | |
| 34 | /** |
| 35 | * Add a typeface to the cache. This ref()s the typeface, so that the |
| 36 | * cache is also an owner. Later, if we need to purge the cache, it will |
| 37 | * unref() typefaces whose refcnt is 1 (meaning only the cache is an owner). |
| 38 | */ |
| 39 | static void Add(SkTypeface*, SkTypeface::Style requested); |
| 40 | |
| 41 | /** |
| 42 | * Search the cache for a typeface with the specified fontID (uniqueID). |
| 43 | * If one is found, return it (its reference count is unmodified). If none |
| 44 | * is found, return NULL. |
| 45 | */ |
| 46 | static SkTypeface* FindByID(SkFontID fontID); |
| 47 | |
| 48 | /** |
| 49 | * Iterate through the cache, calling proc(typeface, ctx) with each |
| 50 | * typeface. If proc returns true, then we return that typeface (its |
| 51 | * reference count is unmodified). If it never returns true, we return NULL. |
| 52 | */ |
| 53 | static SkTypeface* FindByProc(FindProc proc, void* ctx); |
| 54 | |
| 55 | /** |
| 56 | * Debugging only: dumps the status of the typefaces in the cache |
| 57 | */ |
| 58 | static void Dump(); |
| 59 | |
| 60 | private: |
| 61 | static SkTypefaceCache& Get(); |
| 62 | |
| 63 | void add(SkTypeface*, SkTypeface::Style requested); |
| 64 | SkTypeface* findByID(SkFontID findID) const; |
| 65 | SkTypeface* findByProc(FindProc proc, void* ctx) const; |
| 66 | void purge(int count); |
| 67 | |
| 68 | struct Rec { |
| 69 | SkTypeface* fFace; |
| 70 | SkTypeface::Style fRequestedStyle; |
| 71 | }; |
| 72 | SkTDArray<Rec> fArray; |
| 73 | }; |
| 74 | |
| 75 | #endif |