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