| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
#include <algorithm> |
| 24 |
|
| 25 |
// 3rd party library includes |
| 26 |
#include <SDL.h> |
| 27 |
#include <SDL_image.h> |
| 28 |
|
| 29 |
// FIFE includes |
| 30 |
// These includes are split up in two parts, separated by one empty line |
| 31 |
// First block: files included from the FIFE root src directory |
| 32 |
// Second block: files included from the same folder |
| 33 |
#include "util/base/exception.h" |
| 34 |
#include "util/structures/rect.h" |
| 35 |
#include "util/utf8/utf8.h" |
| 36 |
#include "video/image.h" |
| 37 |
#include "video/renderbackend.h" |
| 38 |
|
| 39 |
#include "imagefontbase.h" |
| 40 |
|
| 41 |
namespace FIFE { |
| 42 |
|
| 43 |
ImageFontBase::ImageFontBase() : FontBase(), m_height(0) { |
| 44 |
} |
| 45 |
|
| 46 |
ImageFontBase::~ImageFontBase() { |
| 47 |
type_glyphs::iterator i = m_glyphs.begin(); |
| 48 |
for(; i != m_glyphs.end(); ++i) { |
| 49 |
SDL_FreeSurface(i->second.surface); |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
int32_t ImageFontBase::getWidth(const std::string& text) const { |
| 55 |
int32_t w = 0; |
| 56 |
std::string::const_iterator text_it = text.begin(); |
| 57 |
while(text_it != text.end()) { |
| 58 |
uint32_t codepoint = utf8::next(text_it,text.end()); |
| 59 |
type_glyphs::const_iterator it = m_glyphs.find( codepoint ); |
| 60 |
|
| 61 |
if( it != m_glyphs.end() ) { |
| 62 |
w += it->second.surface->w + getGlyphSpacing(); |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
if( m_placeholder.surface ) { |
| 67 |
w += m_placeholder.surface->w + getGlyphSpacing(); |
| 68 |
} |
| 69 |
} |
| 70 |
return w; |
| 71 |
} |
| 72 |
|
| 73 |
int32_t ImageFontBase::getHeight() const { |
| 74 |
return m_height; |
| 75 |
} |
| 76 |
|
| 77 |
SDL_Surface *ImageFontBase::renderString(const std::string& text) { |
| 78 |
SDL_Surface *surface = SDL_CreateRGBSurface(0, |
| 79 |
getWidth(text),getHeight(),32, |
| 80 |
RMASK, GMASK, BMASK ,AMASK); |
| 81 |
|
| 82 |
SDL_FillRect(surface,0,0x00000000); |
| 83 |
|
| 84 |
SDL_Rect dst; |
| 85 |
dst.x = dst.y = 0; |
| 86 |
s_glyph *glyph = 0; |
| 87 |
|
| 88 |
std::string::const_iterator text_it = text.begin(); |
| 89 |
while(text_it != text.end()) { |
| 90 |
uint32_t codepoint = utf8::next(text_it,text.end()); |
| 91 |
type_glyphs::iterator it = m_glyphs.find( codepoint ); |
| 92 |
|
| 93 |
if( it == m_glyphs.end() ) { |
| 94 |
if( !m_placeholder.surface ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
glyph = &m_placeholder; |
| 98 |
} else { |
| 99 |
glyph = &(it->second); |
| 100 |
} |
| 101 |
dst.y = glyph->offset.y; |
| 102 |
dst.x += glyph->offset.x; |
| 103 |
|
| 104 |
SDL_BlitSurface(glyph->surface,0,surface,&dst); |
| 105 |
dst.x += glyph->surface->w + getGlyphSpacing(); |
| 106 |
} |
| 107 |
|
| 108 |
return surface; |
| 109 |
} |
| 110 |
|
| 111 |
void ImageFontBase::setColor(uint8_t r,uint8_t g,uint8_t b, uint8_t a) { |
| 112 |
} |
| 113 |
} |