font_cache.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: font_cache.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-common.
00007  *
00008  * uds-plugin-common is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-common is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <math.h>
00028 
00029 #include "log.h"
00030 #include "font_cache.h"
00031 
00032 FontCache::FontCache(void)
00033 {
00034 }
00035 
00036 FontCache::FontCache(const FontCache& r)
00037 {
00038 }
00039 
00040 FontCache::~FontCache(void)
00041 {
00042     clear();
00043 }
00044 
00045 void FontCache::remove_smallest()
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 }
00079 
00080 void FontCache::clear()
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 }
00093 
00094 FT_BitmapGlyph FontCache::get_glyph(FT_Face face, const unsigned int hash_code, int ch)
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 }
Generated by  doxygen 1.6.2-20100208