Cached font glyphs. The class is designed to contain multiple font glyphs. For every font glyph, The caller should provide a hash code. More...
#include <font_cache.h>
Public Member Functions | |
~FontCache () | |
void | remove_smallest () |
void | clear () |
FT_BitmapGlyph | get_glyph (FT_Face face, const unsigned int hash_code, int ch) |
Static Public Member Functions | |
static FontCache & | instance () |
Cached font glyphs. The class is designed to contain multiple font glyphs. For every font glyph, The caller should provide a hash code.
Copyright (C) 2008 iRex Technologies B.V. All rights reserved.
Definition at line 46 of file font_cache.h.
FontCache::~FontCache | ( | void | ) |
Definition at line 40 of file font_cache.cpp.
References clear().
00041 { 00042 clear(); 00043 }
void FontCache::clear | ( | ) |
Definition at line 80 of file font_cache.cpp.
Referenced by text::TextView::set_font_size(), and ~FontCache().
00081 { 00082 for(FontIter it = cache.begin(); it != cache.end(); ++it) 00083 { 00084 for(GlyphIter t = it->second.begin(); t != it->second.end(); ++t) 00085 { 00086 FT_Done_Glyph(t->second); 00087 } 00088 it->second.clear(); 00089 } 00090 cache.clear(); 00091 00092 }
FT_BitmapGlyph FontCache::get_glyph | ( | FT_Face | face, | |
const unsigned int | hash_code, | |||
int | ch | |||
) |
Definition at line 94 of file font_cache.cpp.
References ERRORPRINTF.
Referenced by render_single_line().
00095 { 00096 FT_UInt index = ch; 00097 00098 // check hash code at first 00099 CodeIter ci = codes.find(hash_code); 00100 if (ci == codes.end()) 00101 { 00102 codes[hash_code] = 0; 00103 } 00104 00105 // search 00106 FontIter iter = cache.find(hash_code); 00107 if (iter != cache.end()) 00108 { 00109 GlyphIter t = iter->second.find(index); 00110 if (t != iter->second.end()) 00111 { 00112 return reinterpret_cast<FT_BitmapGlyph>(t->second); 00113 } 00114 } 00115 ++codes[hash_code]; 00116 00117 // load glyph 00118 if (FT_Load_Glyph(face, index, FT_LOAD_DEFAULT)) 00119 { 00120 // ERRORPRINTF("Unable to load glyph!"); 00121 return 0; 00122 } 00123 00124 // get glyph 00125 FT_Glyph glyph; 00126 if (FT_Get_Glyph(face->glyph, &glyph)) 00127 { 00128 // ERRORPRINTF("Unable to copy glyph!"); 00129 return 0; 00130 } 00131 00132 // convert to bitmap 00133 if (glyph->format != FT_GLYPH_FORMAT_BITMAP) 00134 { 00135 if (FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1)) 00136 { 00137 ERRORPRINTF("Unable to render glyph!"); 00138 return 0; 00139 } 00140 } 00141 00142 // cache it 00143 cache[hash_code][index] = glyph; 00144 00145 return (FT_BitmapGlyph)glyph; 00146 }
static FontCache& FontCache::instance | ( | ) | [inline, static] |
Definition at line 50 of file font_cache.h.
Referenced by render_single_line(), and text::TextView::set_font_size().
00051 { 00052 static FontCache _instance; 00053 return _instance; 00054 }
void FontCache::remove_smallest | ( | ) |
Definition at line 45 of file font_cache.cpp.
00046 { 00047 // it's necessary to maintain at least one kind of glyph 00048 if (codes.size() <= 1) 00049 { 00050 return; 00051 } 00052 00053 // get smallest one 00054 UINT32 count = (UINT32)-1; 00055 CodeIter smallest = codes.begin(); 00056 for(CodeIter ci = codes.begin(); ci != codes.end(); ++ci) 00057 { 00058 if (count > ci->second) 00059 { 00060 smallest = ci; 00061 count = ci->second; 00062 } 00063 } 00064 UINT32 code = smallest->first; 00065 codes.erase(smallest); 00066 00067 // release them 00068 FontIter it = cache.find(code); 00069 if (it != cache.end()) 00070 { 00071 for(GlyphIter t = it->second.begin(); t != it->second.end(); ++t) 00072 { 00073 FT_Done_Glyph(t->second); 00074 } 00075 it->second.clear(); 00076 cache.erase(it); 00077 } 00078 }