/*************************************************************************** * Copyright (C) 2004 by Christoph Freundl * * Christoph.Freundl@informatik.uni-erlangen.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "fontfactory.h" #include namespace SDLwidgets { FontFactory* FontFactory::_instance = 0; FontFactory* FontFactory::getInstance() { if ( !_instance ) _instance = new FontFactory; return _instance; } void FontFactory::deleteInstance() { if ( _instance ) delete _instance; _instance = 0; } FontFactory::FontFactory() { _font_path = "."; if ( TTF_Init() == -1 ) throw; } FontFactory::~FontFactory() { for ( FontMap::iterator font = _font_map.begin(); font != _font_map.end(); font++ ) { SizeMap* size_map = font->second; for ( SizeMap::iterator size = size_map->begin(); size != size_map->end(); size++ ) { TTF_CloseFont( size->second ); } } TTF_Quit(); } TTF_Font* FontFactory::createFont( std::string name, int size ) { if ( _font_map.find( name ) == _font_map.end() ) _font_map[name] = new SizeMap; SizeMap* size_map = _font_map[name]; if ( size_map->find( size ) == size_map->end() ) { std::string font_file = _font_path + '/' + name; std::cerr << "FontFactory::createFont: trying to load font '" << font_file.c_str() << "'\n"; (*size_map)[size] = TTF_OpenFont( font_file.c_str(), size ); if( (*size_map)[size] ) { std::cerr << "FontFactory::createFont: success\n"; } else { std::cerr << "FontFactory::createFont: could not load font '" << font_file.c_str() << "'\n"; } } return (*size_map)[size]; } }