blob: ce351e5fc4d80e9e154261a51153d58f9bff91d9 [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:
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
60private:
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