blob: b6cab8163fa156580b38bb9934c0172d80fb5365 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.com2f3dc9d2011-05-02 17:33:45 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.com2f3dc9d2011-05-02 17:33:45 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.com2f3dc9d2011-05-02 17:33:45 +000011#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
24class SkTypefaceCache {
25public:
bungeman@google.comee51d1a2012-02-16 12:40:48 +000026 /**
27 * 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
bungeman@google.coma02bc152012-05-16 18:21:56 +000029 * not additionally ref()ed. The typeface may be in the disposed state.
bungeman@google.comee51d1a2012-02-16 12:40:48 +000030 */
reed@google.com2f3dc9d2011-05-02 17:33:45 +000031 typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context);
32
33 /**
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
bungeman@google.comee51d1a2012-02-16 12:40:48 +000041 * 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
43 * unref()ed.
reed@google.com2f3dc9d2011-05-02 17:33:45 +000044 */
bungeman@google.coma02bc152012-05-16 18:21:56 +000045 static void Add(SkTypeface*,
46 SkTypeface::Style requested,
47 bool strong = true);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000048
49 /**
50 * 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
bungeman@google.comee51d1a2012-02-16 12:40:48 +000052 * is found, return NULL. The reference count is unmodified as it is
53 * assumed that the stack will contain a ref to the typeface.
reed@google.com2f3dc9d2011-05-02 17:33:45 +000054 */
55 static SkTypeface* FindByID(SkFontID fontID);
56
57 /**
58 * Iterate through the cache, calling proc(typeface, ctx) with each
bungeman@google.comee51d1a2012-02-16 12:40:48 +000059 * typeface. If proc returns true, then we return that typeface (this
60 * ref()s the typeface). If it never returns true, we return NULL.
reed@google.com2f3dc9d2011-05-02 17:33:45 +000061 */
bungeman@google.comee51d1a2012-02-16 12:40:48 +000062 static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000063
64 /**
bungeman@google.comee51d1a2012-02-16 12:40:48 +000065 * 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.
67 * This function is exposed for clients that explicitly want to purge the
68 * cache (e.g. to look for leaks).
reed@google.comd40da642011-10-20 13:18:37 +000069 */
70 static void PurgeAll();
71
72 /**
reed@google.com2f3dc9d2011-05-02 17:33:45 +000073 * Debugging only: dumps the status of the typefaces in the cache
74 */
75 static void Dump();
76
77private:
78 static SkTypefaceCache& Get();
79
bungeman@google.coma02bc152012-05-16 18:21:56 +000080 void add(SkTypeface*, SkTypeface::Style requested, bool strong = true);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000081 SkTypeface* findByID(SkFontID findID) const;
bungeman@google.coma02bc152012-05-16 18:21:56 +000082 SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const;
reed@google.com2f3dc9d2011-05-02 17:33:45 +000083 void purge(int count);
reed@google.comd40da642011-10-20 13:18:37 +000084 void purgeAll();
reed@google.com2f3dc9d2011-05-02 17:33:45 +000085
86 struct Rec {
87 SkTypeface* fFace;
bungeman@google.coma02bc152012-05-16 18:21:56 +000088 bool fStrong;
reed@google.com2f3dc9d2011-05-02 17:33:45 +000089 SkTypeface::Style fRequestedStyle;
90 };
91 SkTDArray<Rec> fArray;
92};
93
94#endif