00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "wgui_include_config.h"
00026 #include "wg_fontengine.h"
00027 #include "wg_error.h"
00028 #include "wg_application.h"
00029
00030
00031 namespace wGui {
00032
00033
00034 FT_Library CFontEngine::m_FTLibrary;
00035 bool CFontEngine::m_bFTLibraryLoaded = false;
00036
00037
00038 CFontEngine::CFontEngine(const std::wstring& sFontFileName, unsigned char FontSize)
00039 {
00040 if (!m_bFTLibraryLoaded)
00041 {
00042 if (FT_Init_FreeType(&m_FTLibrary))
00043 {
00044 throw(Wg_Ex_FreeType(L"CFontEngine::CFontEngine : Unable to initialize FreeType library."));
00045 }
00046 m_bFTLibraryLoaded = true;
00047 }
00048 if (FT_New_Face(m_FTLibrary, stdex::ToMbString(sFontFileName).c_str(), 0, &m_FontFace))
00049 {
00050 throw(Wg_Ex_FreeType(L"CFontEngine::CFontEngine : Unable to create font face."));
00051 }
00052 if (FT_Set_Char_Size(m_FontFace, 0, FontSize * 64, 0, 0))
00053 {
00054 throw(Wg_Ex_FreeType(L"CFontEngine::CFontEngine : Unable to set character size."));
00055 }
00056 CApplication::Instance()->GetApplicationLog().
00057 AddLogEntry(L"CFontEngine - Loaded new font : " + stdex::itoa(FontSize) + L" point, " + sFontFileName, APP_LOG_INFO);
00058 }
00059
00060
00061 CFontEngine::~CFontEngine(void)
00062 {
00063
00064 FT_Done_Face(m_FontFace);
00065 }
00066
00067 FT_BitmapGlyphRec* CFontEngine::RenderGlyph(wchar_t Char)
00068 {
00069 std::map<wchar_t, FT_BitmapGlyphRec>::iterator glyphIter = m_CachedGlyphMap.find(Char);
00070 if (glyphIter == m_CachedGlyphMap.end())
00071 {
00072 if (FT_Load_Char(m_FontFace, Char, FT_LOAD_DEFAULT))
00073 {
00074 throw(Wg_Ex_FreeType(L"CFontEngine::RenderGlyph : Unable to render glyph."));
00075 }
00076 FT_Glyph glyph;
00077 if (FT_Get_Glyph(m_FontFace->glyph, &glyph))
00078 {
00079 throw(Wg_Ex_FreeType(L"CFontEngine::RenderGlyph : Unable to copy glyph."));
00080 }
00081 if (FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1))
00082 {
00083 throw(Wg_Ex_FreeType(L"CFontEngine::RenderGlyph : Unable to render glyph."));
00084 }
00085 glyphIter = m_CachedGlyphMap.insert(std::make_pair(Char, *reinterpret_cast<FT_BitmapGlyph>(glyph))).first;
00086 }
00087 return &(glyphIter->second);
00088 }
00089
00090 FT_Glyph_Metrics* CFontEngine::GetMetrics(wchar_t Char)
00091 {
00092 std::map<wchar_t, FT_Glyph_Metrics>::iterator glyphIter = m_CachedMetricsMap.find(Char);
00093 if (glyphIter == m_CachedMetricsMap.end())
00094 {
00095 if (FT_Load_Char(m_FontFace, Char, FT_LOAD_DEFAULT))
00096 {
00097 throw(Wg_Ex_FreeType(L"CFontEngine::RenderGlyph : Unable to render glyph."));
00098 }
00099 glyphIter = m_CachedMetricsMap.insert(std::make_pair(Char, m_FontFace->glyph->metrics)).first;
00100 }
00101 return &(glyphIter->second);
00102 }
00103
00104 }