FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
gui_font.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 #include <guichan.hpp>
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "util/structures/rect.h"
00032 #include "video/image.h"
00033 #include "video/renderbackend.h"
00034 
00035 #include "gui_font.h"
00036 
00037 namespace FIFE {
00038     GuiFont::GuiFont(AbstractFont* font): m_font(font) {
00039         assert(font);
00040     }
00041 
00042     GuiFont::~GuiFont() {
00043         delete m_font;
00044     }
00045 
00046     int GuiFont::getStringIndexAt(const std::string& text, int x) const {
00047         return m_font->getStringIndexAt(text, x);
00048     }
00049 
00050     void GuiFont::drawString(gcn::Graphics* graphics, const std::string& text, int x, int y) {
00051         if (text == "") {
00052             return;
00053         }
00054 
00055         int yoffset = getRowSpacing() / 2;
00056 
00057         const gcn::ClipRectangle& clip = graphics->getCurrentClipArea();
00058         FIFE::Rect rect;
00059         rect.x = x + clip.xOffset;
00060         rect.y = y + clip.yOffset + yoffset;
00061         rect.w = getWidth(text);
00062         rect.h = getHeight();
00063 
00064         if (!rect.intersects(Rect(clip.x,clip.y,clip.width,clip.height)) ) {
00065             return;
00066         }
00067 
00068         Image* image = getAsImage(text);
00069         image->render(rect);
00070     }
00071 
00072     void GuiFont::drawMultiLineString(gcn::Graphics* graphics, const std::string& text, int x, int y) {
00073         if (text == "") {
00074             return;
00075         }
00076 
00077         int yoffset = getRowSpacing() / 2;
00078 
00079         const gcn::ClipRectangle& clip = graphics->getCurrentClipArea();
00080 
00081         Image* image = getAsImageMultiline(text);
00082 
00083         FIFE::Rect rect;
00084         rect.x = x + clip.xOffset;
00085         rect.y = y + clip.yOffset + yoffset;
00086         rect.w = image->getWidth();
00087         rect.h = image->getHeight();
00088         if (!rect.intersects(Rect(clip.x,clip.y,clip.width,clip.height)) ) {
00089             return;
00090         }
00091         image->render(rect);
00092     }
00093 
00094     void GuiFont::setRowSpacing (int spacing) {
00095         m_font->setRowSpacing(spacing);
00096     }
00097 
00098     int GuiFont::getRowSpacing() const {
00099         return m_font->getRowSpacing();
00100     }
00101 
00102     void GuiFont::setGlyphSpacing(int spacing) {
00103         m_font->setGlyphSpacing(spacing);
00104     }
00105 
00106     int GuiFont::getGlyphSpacing() const {
00107         return m_font->getGlyphSpacing();
00108     }
00109 
00110     void GuiFont::setAntiAlias(bool antiAlias) {
00111         m_font->setAntiAlias(antiAlias);
00112     }
00113 
00114     bool GuiFont::isAntiAlias() {
00115         return m_font->isAntiAlias();
00116     }
00117 
00118     Image* GuiFont::getAsImage(const std::string& text) {
00119         return m_font->getAsImage(text);
00120     }
00121 
00122     Image* GuiFont::getAsImageMultiline(const std::string& text) {
00123         return m_font->getAsImageMultiline(text);
00124     }
00125 
00126     std::string GuiFont::splitTextToWidth (const std::string& text, int render_width) {
00127         return m_font->splitTextToWidth(text,render_width);
00128     }
00129 
00130     void GuiFont::setColor(uint8_t r,uint8_t g,uint8_t b, uint8_t a) {
00131         m_font->setColor(r, g, b, a);
00132     }
00133 
00134     SDL_Color GuiFont::getColor() const {
00135         return m_font->getColor();
00136     }
00137 
00138     int GuiFont::getWidth(const std::string& text) const {
00139         return m_font->getWidth(text);
00140     }
00141 
00142     int GuiFont::getHeight() const {
00143         return m_font->getHeight();
00144     }
00145 
00146     void GuiFont::invalidate() {
00147         m_font->invalidate();
00148     }
00149 }