wg_fontengine.cpp

00001 // wg_fontengine.cpp
00002 //
00003 // CFontEngine implementation
00004 //
00005 //
00006 // Copyright (c) 2002-2004 Rob Wiskow
00007 // rob-dev@boxedchaos.com
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 // Static members
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 }

Generated on Wed May 16 23:11:25 2007 for wGui by  doxygen 1.5.1