1 #ifndef YFONTCACHE_H
2 #define YFONTCACHE_H
3 
4 #include "yarray.h"
5 
6 class YFontBase;
7 
8 class YFontStore {
9 public:
YFontStore(const char * s,unsigned long h,YFontBase * f)10     YFontStore(const char* s, unsigned long h, YFontBase* f)
11         : name(s), hash(h), font(f) { }
~YFontStore()12     ~YFontStore() { delete font; }
match(const char * s,unsigned long h)13     bool match(const char* s, unsigned long h) const {
14         return h == hash && 0 == strcmp(s, name);
15     }
getFont()16     YFontBase* getFont() const { return font; }
17 private:
18     const char* name;
19     unsigned long hash;
20     YFontBase* font;
21 };
22 
23 class YFontCache {
24 public:
lookup(const char * name)25     YFontBase* lookup(const char* name) {
26         unsigned long hash = strhash(name);
27         for (YFontStore* store : storage) {
28             if (store->match(name, hash)) {
29                 return store->getFont();
30             }
31         }
32         return nullptr;
33     }
store(const char * name,YFontBase * font)34     void store(const char* name, YFontBase* font) {
35         unsigned long hash = strhash(name);
36         storage.append(new YFontStore(name, hash, font));
37     }
clear()38     void clear() {
39         storage.clear();
40     }
41 private:
42     YObjectArray<YFontStore> storage;
43 };
44 
45 extern class YFontCache fontCache;
46 
47 #endif
48