1 :
2 : /*
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.
7 : */
8 :
9 :
10 :
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 0 : 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 : * This will unref all of the typefaces in the cache. Normally this is
57 : * handled automatically as needed. This function is exposed for clients
58 : * that explicitly want to purge the entire cache (e.g. to look for leaks).
59 : */
60 : static void PurgeAll();
61 :
62 : /**
63 : * Debugging only: dumps the status of the typefaces in the cache
64 : */
65 : static void Dump();
66 :
67 : private:
68 : static SkTypefaceCache& Get();
69 :
70 : void add(SkTypeface*, SkTypeface::Style requested);
71 : SkTypeface* findByID(SkFontID findID) const;
72 : SkTypeface* findByProc(FindProc proc, void* ctx) const;
73 : void purge(int count);
74 : void purgeAll();
75 :
76 : struct Rec {
77 : SkTypeface* fFace;
78 : SkTypeface::Style fRequestedStyle;
79 : };
80 : SkTDArray<Rec> fArray;
81 : };
82 :
83 : #endif
|