2012-08-29 18:53:53 +00:00
# ifndef _FONT_H_
# define _FONT_H_
# include <string>
2012-09-10 18:10:59 +00:00
# include "platform.h"
# include GLHEADER
2012-08-29 18:53:53 +00:00
# include <ft2build.h>
# include FT_FREETYPE_H
2013-07-17 03:41:39 +00:00
# include <Eigen/Dense>
2013-07-09 05:44:24 +00:00
# include "resources/ResourceManager.h"
2013-07-02 07:53:23 +00:00
class TextCache ;
2012-08-29 18:53:53 +00:00
2013-07-03 07:54:55 +00:00
# define FONT_SIZE_SMALL ((unsigned int)(0.035f * Renderer::getScreenHeight()))
# define FONT_SIZE_MEDIUM ((unsigned int)(0.045f * Renderer::getScreenHeight()))
# define FONT_SIZE_LARGE ((unsigned int)(0.1f * Renderer::getScreenHeight()))
2012-10-25 23:23:26 +00:00
//A TrueType Font renderer that uses FreeType and OpenGL.
//The library is automatically initialized when it's needed.
2013-07-09 05:44:24 +00:00
class Font : public IReloadable
2012-08-29 18:53:53 +00:00
{
public :
static void initLibrary ( ) ;
2013-07-09 05:44:24 +00:00
static std : : shared_ptr < Font > get ( ResourceManager & rm , const std : : string & path , int size ) ;
2012-08-29 18:53:53 +00:00
~ Font ( ) ;
FT_Face face ;
2012-09-07 21:44:07 +00:00
//contains sizing information for every glyph.
2012-08-29 18:53:53 +00:00
struct charPosData {
int texX ;
int texY ;
int texW ;
int texH ;
2013-06-27 10:30:04 +00:00
float advX ; //!<The horizontal distance to advance to the next character after this one
float advY ; //!<The vertical distance to advance to the next character after this one
2012-08-29 18:53:53 +00:00
2013-06-27 10:30:04 +00:00
float bearingX ; //!<The horizontal distance from the cursor to the start of the character
float bearingY ; //!<The vertical distance from the cursor to the start of the character
2012-08-29 18:53:53 +00:00
} ;
charPosData charData [ 128 ] ;
GLuint textureID ;
2013-07-10 11:29:43 +00:00
TextCache * buildTextCache ( const std : : string & text , float offsetX , float offsetY , unsigned int color ) ;
2013-07-02 07:53:23 +00:00
void renderTextCache ( TextCache * cache ) ;
//Create a TextCache, render with it, then delete it. Best used for short text or text that changes frequently.
2013-07-10 11:29:43 +00:00
void drawText ( std : : string text , const Eigen : : Vector2f & offset , unsigned int color ) ;
Eigen : : Vector2f sizeText ( std : : string text ) const ; //Sets the width and height of a given string to supplied pointers. A dimension is skipped if its pointer is NULL.
2013-07-03 07:54:55 +00:00
2013-08-22 01:08:36 +00:00
std : : string wrapText ( std : : string text , float xLen ) const ;
2013-07-10 11:29:43 +00:00
void drawWrappedText ( std : : string text , const Eigen : : Vector2f & offset , float xLen , unsigned int color ) ;
Eigen : : Vector2f sizeWrappedText ( std : : string text , float xLen ) const ;
2013-09-07 22:43:36 +00:00
Eigen : : Vector2f getWrappedTextCursorOffset ( std : : string text , float xLen , int cursor ) const ;
2013-07-03 07:54:55 +00:00
2013-07-10 11:29:43 +00:00
void drawCenteredText ( std : : string text , float xOffset , float y , unsigned int color ) ;
int getHeight ( ) const ;
2012-10-24 15:28:37 +00:00
2013-07-09 05:44:24 +00:00
void unload ( const ResourceManager & rm ) override ;
void reload ( const ResourceManager & rm ) override ;
2012-08-29 18:53:53 +00:00
2013-07-10 11:29:43 +00:00
int getSize ( ) const ;
2012-10-31 14:46:06 +00:00
static std : : string getDefaultPath ( ) ;
2012-09-07 21:44:07 +00:00
private :
2012-08-29 18:53:53 +00:00
static int getDpiX ( ) ;
static int getDpiY ( ) ;
2012-10-24 15:28:37 +00:00
2012-08-29 18:53:53 +00:00
static FT_Library sLibrary ;
2012-10-24 15:28:37 +00:00
static bool libraryInitialized ;
2012-08-29 18:53:53 +00:00
2013-07-09 05:44:24 +00:00
static std : : map < std : : pair < std : : string , int > , std : : weak_ptr < Font > > sFontMap ;
Font ( const ResourceManager & rm , const std : : string & path , int size ) ;
void init ( ResourceData data ) ;
void deinit ( ) ;
2013-07-03 07:54:55 +00:00
void buildAtlas ( ResourceData data ) ; //Builds a "texture atlas," one big OpenGL texture with glyphs 32 to 128.
2012-08-29 18:53:53 +00:00
2012-09-07 21:44:07 +00:00
int textureWidth ; //OpenGL texture width
int textureHeight ; //OpenGL texture height
2012-08-29 21:52:25 +00:00
int mMaxGlyphHeight ;
2013-06-12 12:42:09 +00:00
float fontScale ; //!<Font scale factor. It is > 1.0 if the font would be to big for the texture
2012-10-24 15:28:37 +00:00
int mSize ;
2013-07-09 05:44:24 +00:00
const std : : string mPath ;
2012-08-29 18:53:53 +00:00
} ;
2013-07-02 07:53:23 +00:00
class TextCache
{
public :
struct Vertex
{
2013-07-10 11:29:43 +00:00
Eigen : : Vector2f pos ;
Eigen : : Vector2f tex ;
2013-07-02 07:53:23 +00:00
} ;
2013-08-22 20:29:50 +00:00
struct CacheMetrics
{
Eigen : : Vector2f size ;
} metrics ;
2013-07-02 07:53:23 +00:00
void setColor ( unsigned int color ) ;
2013-08-22 20:29:50 +00:00
TextCache ( int verts , Vertex * v , GLubyte * c , const CacheMetrics & m ) ;
2013-07-02 07:53:23 +00:00
~ TextCache ( ) ;
2013-08-22 01:08:36 +00:00
int vertCount ;
Vertex * verts ;
GLubyte * colors ;
2013-07-02 07:53:23 +00:00
} ;
2012-08-29 18:53:53 +00:00
# endif