Commiting whatever state I got that gui data grid widget for archival and fond memori...
[fail.git] / src / scenegraph / Font.cpp
blobf2330da6e1f9efe5421ce46d3e43b5dd0d79dda2
1 /*
2 Fail game engine
3 Copyright 2007 Antoine Chavasse <a.chavasse@gmail.com>
5 This file is part of Fail.
7 Fail is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3
9 as published by the Free Software Foundation.
11 Fail 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "scenegraph/Font.h"
20 #include <stdexcept>
21 #include <FTGL/FTGLTextureFont.h>
22 #include <GL/gl.h>
24 using namespace fail;
25 using namespace fail::scenegraph;
27 // TODO: reimplement text rendering from scratch. Don't use FTGL, use freetype2 directly
28 // and manage the textures manually (maybe implement the helpers to manager combined texture first,
29 // as this would be a typical use case)
30 // Decide on a clever way to render a bunch of rectangle instances, each with their own uv transformations
31 // if possible (to render a bunch of quads all sourcing different parts of the same texture in one go -
32 // useful for fast text rendering, and could probably be useful for the gui aswell. basically
33 // for any assemblage of rectangular bitmaps)
34 Font::Font( std::string Filename_, uint32_t Size_ )
36 std::string fontpath( INSTALL_PREFIX "/share/fail/fonts/" );
37 fontpath += Filename_;
38 m_pFTGLFont = new FTGLTextureFont( fontpath.c_str() );
39 //return;
41 // TODO: Need more specific exceptions someday, as well as a way to wrap them in the idl and lua,
42 // which shouldn't be difficult, but this is going to the pile of stuff to fix later for now.
43 if( m_pFTGLFont->Error() )
44 throw std::bad_alloc();
45 m_pFTGLFont->FaceSize( Size_ );
47 // Find out a proper font height that will work properly for regular characters.
48 // The lineheight returned by the font include seldom used characters and it's way bigger
49 // than necessary.
50 float llx, lly, llz, urx, ury, urz;
51 m_pFTGLFont->BBox( "ÉABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", llx, lly, llz, urx, ury, urz);
52 m_Height = floor( ury - lly + 1.f + 0.5f );
54 m_Offset = -ury;
57 Font::~Font()
59 delete m_pFTGLFont;
62 void Font::drawText( const std::string& Text ) const
64 // TODO: support wstring... Need to add it all over aic, the wrappers and stuff though.
65 // I don't even know if lua supports this.
67 glEnable( GL_TEXTURE_2D );
68 glPushMatrix();
69 glTranslatef( 0.f, 0.f, m_Offset );
70 glRotatef( 90.f, 1.f, 0.f, 0.f );
71 m_pFTGLFont->Render( Text.c_str() );
72 glPopMatrix();
75 void Font::drawTextRightClip( const std::string& Text, float ClipPos ) const
77 glEnable( GL_TEXTURE_2D );
78 glPushMatrix();
79 glTranslatef( 0.f, 0.f, m_Offset );
80 glRotatef( 90.f, 1.f, 0.f, 0.f );
82 float pos = 0.f;
83 char tmp[2];
84 tmp[1] = 0;
86 std::string::const_iterator it;
87 for( it = Text.begin(); it != Text.end(); ++it )
89 tmp[0] = *it;
90 float adv = m_pFTGLFont->Advance( tmp );
91 pos += adv;
92 if( pos > ClipPos )
93 break;
94 m_pFTGLFont->Render( tmp );
95 glTranslatef( adv, 0.f, 0.f );
98 glPopMatrix();
101 float Font::getTextWidth( const std::string& Text_ ) const
103 float llx, lly, llz, urx, ury, urz;
104 m_pFTGLFont->BBox( Text_.c_str(), llx, lly, llz, urx, ury, urz );
105 return floor( urx - llx + 0.5f );