1 /** @file lvfont_glyphcache.h
2     @brief font glyph cache interface
3 
4     CoolReader Engine
5 
6     (c) Vadim Lopatin, 2000-2006
7 
8     This source code is distributed under the terms of
9     GNU General Public License.
10 
11     See LICENSE file for details.
12 
13 */
14 
15 #ifndef __LV_FONTGLYPHCACHE_H_INCLUDED__
16 #define __LV_FONTGLYPHCACHE_H_INCLUDED__
17 
18 #include <stdlib.h>
19 #include "crsetup.h"
20 #include "lvtypes.h"
21 #include "lvhashtable.h"
22 #include "../../include/crlocks.h"
23 #define GLYPHCACHE_TABLE_SZ         256
24 
25 struct LVFontGlyphCacheItem;
26 
27 class LVFontGlobalGlyphCache {
28 private:
29     LVFontGlyphCacheItem *head;
30     LVFontGlyphCacheItem *tail;
31     int size;
32     int max_size;
33 
34     void removeNoLock(LVFontGlyphCacheItem *item);
35 
36     void putNoLock(LVFontGlyphCacheItem *item);
37 
38 public:
LVFontGlobalGlyphCache(int maxSize)39     LVFontGlobalGlyphCache(int maxSize)
40             : head(NULL), tail(NULL), size(0), max_size(maxSize) {
41     }
42 
~LVFontGlobalGlyphCache()43     ~LVFontGlobalGlyphCache() {
44         clear();
45     }
46 
47     void put(LVFontGlyphCacheItem *item);
48 
49     void remove(LVFontGlyphCacheItem *item);
50 
51     void refresh(LVFontGlyphCacheItem *item);
52 
53     void clear();
54 };
55 
56 class LVLocalGlyphCacheHashTableStorage
57 {
58     LVHashTable<lUInt32, struct LVFontGlyphCacheItem*> hashTable;
59     LVFontGlobalGlyphCache* m_global_cache;
60     //non-cpyable
61     LVLocalGlyphCacheHashTableStorage();
62     LVLocalGlyphCacheHashTableStorage( const LVLocalGlyphCacheHashTableStorage& );
63     LVLocalGlyphCacheHashTableStorage& operator=( const LVLocalGlyphCacheHashTableStorage& );
64 public:
LVLocalGlyphCacheHashTableStorage(LVFontGlobalGlyphCache * global_cache)65     LVLocalGlyphCacheHashTableStorage(LVFontGlobalGlyphCache *global_cache) :
66         m_global_cache(global_cache), hashTable(GLYPHCACHE_TABLE_SZ) {}
~LVLocalGlyphCacheHashTableStorage()67     ~LVLocalGlyphCacheHashTableStorage() {
68         clear();
69     }
70     LVFontGlyphCacheItem* get(lUInt32 ch);
71     void put(LVFontGlyphCacheItem *item);
72     void remove(LVFontGlyphCacheItem *item);
73     void clear();
74 };
75 
76 class LVLocalGlyphCacheListStorage
77 {
78     LVFontGlobalGlyphCache* m_global_cache;
79     LVFontGlyphCacheItem* head;
80     LVFontGlyphCacheItem* tail;
81     //non-cpyable
82     LVLocalGlyphCacheListStorage();
83     LVLocalGlyphCacheListStorage( const LVLocalGlyphCacheListStorage& );
84     LVLocalGlyphCacheListStorage& operator=( const LVLocalGlyphCacheListStorage& );
85 public:
LVLocalGlyphCacheListStorage(LVFontGlobalGlyphCache * global_cache)86     LVLocalGlyphCacheListStorage(LVFontGlobalGlyphCache *global_cache) :
87          m_global_cache(global_cache), head(), tail() {}
~LVLocalGlyphCacheListStorage()88     ~LVLocalGlyphCacheListStorage() {
89         clear();
90     }
91     LVFontGlyphCacheItem* get(lUInt32 ch);
92     void put(LVFontGlyphCacheItem *item);
93     void remove(LVFontGlyphCacheItem *item);
94     void clear();
95 };
96 
97 template<class S>
98 class LVFontLocalGlyphCache_t {
99 public:
LVFontLocalGlyphCache_t(LVFontGlobalGlyphCache * globalCache)100     LVFontLocalGlyphCache_t(LVFontGlobalGlyphCache *globalCache) : m_storage(globalCache) {
101 
102     }
clear()103     void clear() {
104         FONT_LOCAL_GLYPH_CACHE_GUARD
105         m_storage.clear();
106     }
get(lUInt32 index)107     LVFontGlyphCacheItem *get(lUInt32 index) {
108         FONT_LOCAL_GLYPH_CACHE_GUARD
109         return m_storage.get(index);
110     }
put(LVFontGlyphCacheItem * item)111     void put(LVFontGlyphCacheItem *item) {
112         FONT_LOCAL_GLYPH_CACHE_GUARD
113         m_storage.put(item);
114     }
remove(LVFontGlyphCacheItem * item)115     void remove(LVFontGlyphCacheItem *item) {
116         FONT_LOCAL_GLYPH_CACHE_GUARD
117         m_storage.remove(item);
118     }
119 private:
120     S m_storage;
121 };
122 
123 #if USE_GLYPHCACHE_HASHTABLE == 1
124     typedef LVFontLocalGlyphCache_t<LVLocalGlyphCacheHashTableStorage> LVFontLocalGlyphCache;
125 #else
126     typedef LVFontLocalGlyphCache_t<LVLocalGlyphCacheListStorage> LVFontLocalGlyphCache;
127 #endif
128 
129 #if USE_HARFBUZZ == 1
130     typedef lUInt32 LVFontGlyphCacheKeyType;
131 #else
132     typedef lChar32 LVFontGlyphCacheKeyType;
133 #endif
134 
135 struct LVFontGlyphCacheItem {
136     LVFontGlyphCacheItem *prev_global;
137     LVFontGlyphCacheItem *next_global;
138     LVFontGlyphCacheItem *prev_local;
139     LVFontGlyphCacheItem *next_local;
140     LVFontLocalGlyphCache *local_cache;
141     LVFontGlyphCacheKeyType data;
142     lUInt16 bmp_width;
143     lUInt16 bmp_height;
144     lInt16 origin_x;
145     lInt16 origin_y;
146     lUInt16 advance;
147     lUInt8 bmp[1];
148 
149     //=======================================================================
getSizeLVFontGlyphCacheItem150     int getSize() {
151         return sizeof(LVFontGlyphCacheItem)
152                + (bmp_width * bmp_height - 1) * sizeof(lUInt8);
153     }
154     static LVFontGlyphCacheItem *newItem(LVFontLocalGlyphCache *local_cache, LVFontGlyphCacheKeyType ch_or_index, int w, int h);
155     static void freeItem(LVFontGlyphCacheItem *item);
156 };
157 #endif //__LV_FONTGLYPHCACHE_H_INCLUDED__
158