Back to index

extremetuxracer  0.5beta
font.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  */
00018  
00019 /*
00020  * Note:
00021  * The way we bind fonts to other fonts is a dirty hack and
00022  * lacks resource management / refpointers.
00023  * Therefore we don't delete mp_font in the destructor.
00024  * This isn't a real memleak because we don't delete fonts
00025  * in current ppracer version.
00026  * 
00027  * This will changed with the new resource manager that
00028  * isn't ready yet.
00029  */ 
00030 
00031 #include "font.h"
00032 
00033 #ifdef _WIN32
00034        #include <WTypes.h>
00035 #endif
00036 
00037 #include <GL/gl.h>
00038 #include <iostream>
00039 
00040 #include "FT/FTGLTextureFont.h"
00041 #include "FT/FTGLOutlineFont.h"
00042 
00043 
00044 namespace pp{
00045        
00046 Font::Font(const char *fileName, unsigned int size, const pp::Color &color)
00047  : mp_font(NULL),
00048    m_color(color)
00049 {
00050        mp_font = new FTGLTextureFont(fileName);
00051        
00052        mp_font->FaceSize(size);
00053        mp_font->CharMap(ft_encoding_unicode);    
00054 }
00055 
00056 Font::Font(const char *fileName, unsigned int size, const pp::Color &color, bool isOutline)
00057  : mp_font(NULL),
00058    m_color(color)
00059 {
00060     if(isOutline) {
00061         mp_font = new FTGLOutlineFont(fileName);
00062     }else{
00063         mp_font = new FTGLTextureFont(fileName);
00064     };
00065        
00066        mp_font->FaceSize(size);
00067        mp_font->CharMap(ft_encoding_unicode);    
00068 }
00069 
00070 
00071 
00072 Font::Font(FTFont *font, const pp::Color &color)
00073  : mp_font(font),
00074    m_color(color)
00075 {
00076 }
00077 
00078 Font::~Font()
00079 {
00080        /* we don't have a refpointer yet...      
00081        if(mp_font!=NULL){
00082               delete mp_font;
00083        }
00084        */
00085 }
00086        
00087 void
00088 Font::draw(const char *string, float x, float y)
00089 {
00090        const wchar_t* u_string;
00091        u_string = Font::utf8ToUnicode(string);   
00092 
00093        glPushMatrix();
00094        {
00095               glColor4dv( (double*)&m_color );
00096               glTranslatef(x, y, 0);
00097               mp_font->Render(u_string);
00098        }
00099        glPopMatrix();
00100 
00101        delete u_string;
00102 }
00103 
00104 void
00105 Font::draw(const char* string, pp::Vec2d position)
00106 {
00107        draw(string, position.x, position.y);
00108 }
00109 
00110 void
00111 Font::draw(const wchar_t *string, float x, float y)
00112 {
00113        glPushMatrix();
00114        {
00115               glColor4dv( (double*)&m_color );
00116               glTranslatef(x, y, 0);
00117               mp_font->Render(string);
00118        }
00119        glPopMatrix();
00120 }
00121 
00122 void
00123 Font::draw(const wchar_t *string, pp::Vec2d position)
00124 {
00125        draw(string, position.x, position.y);
00126 }
00127 
00128 float
00129 Font::ascender()
00130 {
00131        return mp_font->Ascender();
00132 }
00133 
00134 float
00135 Font::descender()
00136 {
00137        return mp_font->Descender();
00138 }
00139 
00140 float
00141 Font::advance(const char* string)
00142 {
00143        const wchar_t* u_string;
00144        u_string = Font::utf8ToUnicode(string);   
00145        
00146        float adv = mp_font->Advance(u_string);
00147        
00148        delete u_string;
00149        return adv;   
00150 }
00151 
00152 float
00153 Font::advance(const wchar_t* string)
00154 {
00155        return mp_font->Advance(string);   
00156 }
00157 
00158 pp::Color&
00159 Font::getColor()
00160 {
00161        return m_color;
00162 }
00163 
00164 void Font::setColor(pp::Color& c) {
00165     m_color = c;
00166 }
00167 
00168 
00169 FTFont*
00170 Font::getFTFont()
00171 {
00172        return mp_font;
00173 }
00174        
00175 
00176 //static stuff
00177 
00178 std::map<std::string, pp::Font*> Font::sm_bindings;
00179 
00180 bool
00181 Font::registerFont(const char *binding, const char *fileName, unsigned int size, const pp::Color &color)
00182 {
00183        //todo: check wether the font alrady exists      
00184        sm_bindings[binding] = new pp::Font(fileName, size, color);
00185        return true;
00186 }
00187 
00188 bool
00189 Font::registerFont(const char *binding, const char *fileName, unsigned int size, const pp::Color &color, bool isOutline)
00190 {
00191        //todo: check wether the font alrady exists      
00192        sm_bindings[binding] = new pp::Font(fileName, size, color, isOutline);
00193        return true;
00194 }
00195 
00196 bool
00197 Font::bindFont(const char *binding, const char *fontName)
00198 {
00199        pp::Font *font = pp::Font::get(fontName);
00200        if(font==NULL) return false;
00201               
00202        sm_bindings[binding] = new pp::Font(font->getFTFont(), font->getColor());
00203        return true;
00204 }      
00205 
00206 bool
00207 Font::bindFont(const char *binding, const char *fontName, const pp::Color &color)
00208 {
00209        pp::Font *font = pp::Font::get(fontName);
00210        if(font==NULL) return false;
00211               
00212        sm_bindings[binding] = new pp::Font(font->getFTFont(), color);
00213        return true;
00214 }
00215 
00216 Font*
00217 Font::get(const char* binding)
00218 {
00219        if(binding==NULL){
00220               return NULL;
00221        }
00222        
00223        std::map<std::string, pp::Font*>::iterator it;
00224        it = sm_bindings.find(binding);
00225        if (it != sm_bindings.end()){
00226               return (*it).second;
00227        }else{
00228               return NULL;
00229        }      
00230 }
00231 
00232 void
00233 Font::draw(const char* binding, const char *string, float x, float y)
00234 {
00235        pp::Font* font = pp::Font::get(binding);
00236        
00237        if(font!=NULL){      
00238               font->draw(string, x, y);
00239        }
00240 }
00241 
00242 wchar_t*
00243 Font::utf8ToUnicode(const char* string)
00244 {
00245        wchar_t ch;
00246        int len = strlen(string);
00247        wchar_t *u_string = new wchar_t[len+1];
00248        int j=0;
00249               
00250        for (int i=0; i < len; ++i, ++j ) {
00251               ch = ((const unsigned char *)string)[i];
00252               if ( ch >= 0xF0 ) {
00253                      ch  =  (wchar_t)(string[i]&0x07) << 18;
00254                      ch |=  (wchar_t)(string[++i]&0x3F) << 12;
00255                      ch |=  (wchar_t)(string[++i]&0x3F) << 6;
00256                      ch |=  (wchar_t)(string[++i]&0x3F);
00257               } else
00258               if ( ch >= 0xE0 ) {
00259                      ch  =  (wchar_t)(string[i]&0x0F) << 12;
00260                      ch |=  (wchar_t)(string[++i]&0x3F) << 6;
00261                      ch |=  (wchar_t)(string[++i]&0x3F);
00262               } else
00263               if ( ch >= 0xC0 ) {
00264                      ch  =  (wchar_t)(string[i]&0x1F) << 6;
00265                      ch |=  (wchar_t)(string[++i]&0x3F);
00266               }
00267               u_string[j] = ch;
00268        }
00269        u_string[j] = 0;
00270        
00271        return u_string;
00272 }
00273 
00274 void
00275 Font::utf8ToUnicode(wchar_t* buff, const char* string)
00276 {
00277        wchar_t ch;
00278        int len = strlen(string);
00279        int j=0;
00280               
00281        for (int i=0; i < len; ++i, ++j ) {
00282               ch = ((const unsigned char *)string)[i];
00283               if ( ch >= 0xF0 ) {
00284                      ch  =  (wchar_t)(string[i]&0x07) << 18;
00285                      ch |=  (wchar_t)(string[++i]&0x3F) << 12;
00286                      ch |=  (wchar_t)(string[++i]&0x3F) << 6;
00287                      ch |=  (wchar_t)(string[++i]&0x3F);
00288               } else
00289               if ( ch >= 0xE0 ) {
00290                      ch  =  (wchar_t)(string[i]&0x0F) << 12;
00291                      ch |=  (wchar_t)(string[++i]&0x3F) << 6;
00292                      ch |=  (wchar_t)(string[++i]&0x3F);
00293               } else
00294               if ( ch >= 0xC0 ) {
00295                      ch  =  (wchar_t)(string[i]&0x1F) << 6;
00296                      ch |=  (wchar_t)(string[++i]&0x3F);
00297               }
00298               buff[j] = ch;
00299        }
00300        buff[j] = 0;
00301 }
00302 
00303 
00304 } //namepsace pp