Back to index

supertuxkart  0.5+dfsg1
Public Types | Public Member Functions | Static Public Attributes | Private Attributes
Font Class Reference

#include <font.hpp>

List of all members.

Public Types

enum  FontSize { SMALL = 18, MEDIUM = 24, LARGE = 30 }

Public Member Functions

 Font (const char *fontname)
 Font (const std::string &fontname)
 ~Font ()
void getBBox (const std::string &text, int size, bool italic, float *left, float *right, float *bot, float *top)
void getBBoxMultiLine (const std::string &text, int size, bool italic, float *left, float *right, float *bot, float *top)
void Print (const char *text, int size, int x, int y, const GLfloat *color=NULL, float scale_x=1.0f, float scale_y=1.0f, int left=-1, int right=-1, int top=-1, int bottom=-1, bool doShadow=false)
void Print (std::string const &text, int size, int x, int y, const GLfloat *color=NULL, float scale_x=1.0f, float scale_y=1.0f, int left=-1, int right=-1, int top=-1, int bottom=-1, bool doShadow=false)
void PrintShadow (const char *text, int size, int x, int y, const GLfloat *color=NULL, float scale_x=1.0f, float scale_y=1.0f, int left=-1, int right=-1, int top=-1, int bottom=-1)
void PrintBold (std::string const &text, int size, int x, int y, const GLfloat *color=NULL, float scale_x=1.0f, float scale_y=1.0f, int left=-1, int right=-1, int top=-1, int bottom=-1)

Static Public Attributes

static const int CENTER_OF_SCREEN = -1000001

Private Attributes

fntTexFont * m_fnt
fntRenderer * m_text_out

Detailed Description

Definition at line 27 of file font.hpp.


Member Enumeration Documentation

Enumerator:
SMALL 
MEDIUM 
LARGE 

Definition at line 36 of file font.hpp.

{SMALL=18,   MEDIUM=24,   LARGE=30    };

Constructor & Destructor Documentation

Font::Font ( const char *  fontname)

Definition at line 46 of file font.cpp.

{
    m_fnt      = new fntTexFont(file_manager->getFontFile(fontname).c_str(),
                                GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
    m_text_out = new fntRenderer();
    m_text_out->setFont(m_fnt);
}   // Font

Here is the call graph for this function:

Font::Font ( const std::string &  fontname) [inline]

Definition at line 38 of file font.hpp.

{ Font(fontname.c_str()); }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 55 of file font.cpp.

{
    delete m_text_out;
    delete m_fnt;
}   // ~Font

Member Function Documentation

void Font::getBBox ( const std::string &  text,
int  size,
bool  italic,
float *  left,
float *  right,
float *  bot,
float *  top 
)

Definition at line 191 of file font.cpp.

{
    m_fnt->getBBox(text.c_str(), (float)size, italic, left, right, bot, top);
    if(user_config->m_width<800) {
        float fract=(float)user_config->m_width/800.0f;
        *left  *= fract;
        *right *= fract;
        if(bot) *bot   *= fract;
        if(top) *top   *= fract;
    }

}
void Font::getBBoxMultiLine ( const std::string &  text,
int  size,
bool  italic,
float *  left,
float *  right,
float *  bot,
float *  top 
)

Definition at line 206 of file font.cpp.

{
    // Plib does not handle multi-lines strings as expected. So as a work
    // around we split strings into lines, and compute the size for each
    // line, and take the maximum size at the end.
    std::vector<std::string> s=StringUtils::split(text,'\n');
    m_fnt->getBBox(s[0].c_str(), (float)size, italic, left, right, bot, top);
    for(unsigned int i=1; i<s.size(); i++)
    {
        float l,r,b,t;
        m_fnt->getBBox(s[i].c_str(), (float)size, italic, &l, &r, &b, &t);
        if(left) *left  = std::min(*left,  l);
        if(bot ) *bot   = std::min(*bot,   b);
        if(right)*right = std::max(*right, r); 
        if(top)  *top   = std::max(*top,   t);
    }
    if(user_config->m_width<800) {
        float fract=(float)user_config->m_width/800.0f;
        *left  *= fract;
        *right *= fract;
        if(bot) *bot   *= fract;
        if(top) *top   *= fract;
    }

}

Here is the call graph for this function:

Here is the caller graph for this function:

void Font::Print ( const char *  text,
int  size,
int  x,
int  y,
const GLfloat *  color = NULL,
float  scale_x = 1.0f,
float  scale_y = 1.0f,
int  left = -1,
int  right = -1,
int  top = -1,
int  bottom = -1,
bool  doShadow = false 
)

Definition at line 63 of file font.cpp.

{
    
    // Only scale for lower resolution
    float fontScaling = user_config->m_width<800 ? ((float)user_config->m_width/800.0f) 
                                                 : 1.0f;
    int   sz          = (int)(size*std::max(scale_x,scale_y)*fontScaling);

    float l,r,t,b;
    m_fnt->getBBox(text, (float)sz, 0, &l, &r, &b, &t);
    const int W = (int)((r-l+0.99));
    const int H = (int)((t-b+0.99));

    if(x==CENTER_OF_SCREEN)
    {
        if(left ==-1) left  = 0;
        if(right==-1) right = user_config->m_width-1;
        int width = right-left+1;
        x         = (width - W)/2 + left;
    }

    if(y==CENTER_OF_SCREEN)
    {
        if(top    == -1) top    = user_config->m_height-1;
        if(bottom == -1) bottom = 0;
        int height = top-bottom+1;
        y = (height - H)/2 + bottom;
    }

    m_text_out->begin();
    m_text_out->setPointSize((float)sz);
    if(doShadow)
    {
        m_text_out->start2f((GLfloat)x-2, (GLfloat)y-2);
        glColor4ub(0, 0, 0, 100);
        m_text_out->puts(text);
    }
    m_text_out->start2f((GLfloat)x, (GLfloat)y);

    if( color == NULL )
    {
        glColor4f(1.0f,1.0f,1.0f,1.0f);
    }
    else
    {
        glColor4fv(color);
    }
    m_text_out->puts(text);
    m_text_out->end();

}   // Print

Here is the caller graph for this function:

void Font::Print ( std::string const &  text,
int  size,
int  x,
int  y,
const GLfloat *  color = NULL,
float  scale_x = 1.0f,
float  scale_y = 1.0f,
int  left = -1,
int  right = -1,
int  top = -1,
int  bottom = -1,
bool  doShadow = false 
) [inline]

Definition at line 53 of file font.hpp.

    {
        Print(text.c_str(), size, x, y,
              color, scale_x, scale_y, left, right, top, bottom,
              doShadow);
    }

Here is the call graph for this function:

void Font::PrintBold ( std::string const &  text,
int  size,
int  x,
int  y,
const GLfloat *  color = NULL,
float  scale_x = 1.0f,
float  scale_y = 1.0f,
int  left = -1,
int  right = -1,
int  top = -1,
int  bottom = -1 
)

Definition at line 120 of file font.cpp.

{
    // Only scale for lower resolution
    float fontScaling = user_config->m_width<800 ? ((float)user_config->m_width/800.0f) 
                                                 : 1.0f;
    int   sz          = (int)(size*std::max(scale_x,scale_y)*fontScaling);

    float l,r,t,b;
    getBBoxMultiLine(text, sz, 0, &l, &r, &b, &t);
    const int W = (int)((r-l+0.99));
    const int H = (int)((t-b+0.99));

    if(x==CENTER_OF_SCREEN)
    {
        if(left ==-1) left  = 0;
        if(right==-1) right = user_config->m_width-1;
        int width = right-left+1;
        x         = (width - W)/2 + left;
    }

    if(y==CENTER_OF_SCREEN)
    {
        if(top    == -1) top    = user_config->m_height-1;
        if(bottom == -1) bottom = 0;
        int height = top-bottom+1;
        y = (height - H)/2 + bottom;
    }


    m_text_out->begin();
    m_text_out->setPointSize((float)sz);

    // print shadow
    // ------------
    glColor4f(0.0f,0.0f,0.0f,1.0f);
    float xf=(GLfloat)x+3;
    float yf=(GLfloat)y-3;
    for(float r=-1; r<=0; r+=0.5)
    {
        m_text_out->start2f(xf-r, yf-r);
        m_text_out->puts(text.c_str());
    }

    if( color == NULL )
    {
        glColor4f(1.0f,1.0f,1.0f,1.0f);
    }
    else
    {
        glColor4fv(color);
    }
    xf=(GLfloat)x;
    yf=(GLfloat)y;
    for(float r=-1.0f; r<=0.0f; r+=0.5f)
    {
        // This kind of simulates an outline, but it's not too good
        //if(r==-1.0f || r==0.0f)
        //    glColor4f(0.0f,0.0f,0.0f,1.0f);
        //else
        //    glColor4f(1.0f,1.0f,1.0f,1.0f);

        m_text_out->start2f((GLfloat)x+r, (GLfloat)y+r);
        m_text_out->puts(text.c_str());
    }
    m_text_out->end();

}   // PrintBold

Here is the call graph for this function:

Here is the caller graph for this function:

void Font::PrintShadow ( const char *  text,
int  size,
int  x,
int  y,
const GLfloat *  color = NULL,
float  scale_x = 1.0f,
float  scale_y = 1.0f,
int  left = -1,
int  right = -1,
int  top = -1,
int  bottom = -1 
) [inline]

Definition at line 65 of file font.hpp.

    {
                     Print(text, size, x, y,
                           color, scale_x, scale_y,
                           left, right, top, bottom, true);
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

const int Font::CENTER_OF_SCREEN = -1000001 [static]

Definition at line 35 of file font.hpp.

fntTexFont* Font::m_fnt [private]

Definition at line 30 of file font.hpp.

fntRenderer* Font::m_text_out [private]

Definition at line 31 of file font.hpp.


The documentation for this class was generated from the following files: