Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Font Class Reference

Inheritance diagram for Font:

Inheritance graph
[legend]
Collaboration diagram for Font:

Collaboration graph
[legend]

Detailed Description

TrueType UTF-8 font.

Definition at line 16 of file Font.h.

Public Member Functions

 Font (const Path &file_ttf, int height)
 Create new font from file.
 ~Font ()
int getHeight ()
int calcTextWidth (const std::string &text)
SDL_Surface * renderText (const std::string &text, const SDL_Color &color) const
 Render text with this color.
SDL_Surface * renderTextOutlined (const std::string &text, const SDL_Color &color, int outlineWidth=1) const
 Render text with black outline around font.

Static Public Member Functions

static void init ()
 Prepare font rendering.
static void shutdown ()
 Deinit font subsystem.


Constructor & Destructor Documentation

Font::Font const Path file_ttf,
int  height
 

Create new font from file.

Initialized TTF_Init when necessary.

Parameters:
file_ttf path to ttf file
height font height
Exceptions:
TTFException when cannot open font

Definition at line 25 of file Font.cpp.

00026 {
00027     m_ttfont = TTF_OpenFont(file_ttf.getNative().c_str(), height);
00028     if (!m_ttfont) {
00029         throw TTFException(ExInfo("OpenFont")
00030                 .addInfo("file", file_ttf.getNative()));
00031     }
00032 
00033     //NOTE: bg color will be set to be transparent
00034     SDL_Color bg = {10, 10, 10, 0};
00035     m_bg = bg;
00036 }

Font::~Font  ) 
 

Definition at line 38 of file Font.cpp.

00039 {
00040     TTF_CloseFont(m_ttfont);
00041 }


Member Function Documentation

int Font::calcTextWidth const std::string &  text  ) 
 

Definition at line 66 of file Font.cpp.

00067 {
00068     int w;
00069     TTF_SizeUTF8(m_ttfont, text.c_str(), &w, NULL);
00070     return w;
00071 }

int Font::getHeight  )  [inline]
 

Definition at line 26 of file Font.h.

00026 { return TTF_FontHeight(m_ttfont); }

void Font::init  )  [static]
 

Prepare font rendering.

Exceptions:
TTFException when cannot init SDL_ttf.

Definition at line 48 of file Font.cpp.

00049 {
00050     if (TTF_Init() < 0) {
00051         throw TTFException(ExInfo("Init"));
00052     }
00053 }

SDL_Surface * Font::renderText const std::string &  text,
const SDL_Color &  color
const
 

Render text with this color.

Parameters:
text utf-8 encoded text
color text color
Returns:
new rendered surface
Exceptions:
TTFException when render fails
SDLException when converting fails

Definition at line 82 of file Font.cpp.

00083 {
00084     const char *content = text.c_str();
00085     if (text.empty()) {
00086         content = " ";
00087         LOG_WARNING(ExInfo("empty text to render")
00088                 .addInfo("r", color.r)
00089                 .addInfo("g", color.g)
00090                 .addInfo("b", color.b));
00091     }
00092 
00093     SDL_Surface *raw_surface = TTF_RenderUTF8_Shaded(m_ttfont, content,
00094             color, m_bg);
00095     if (!raw_surface) {
00096         throw TTFException(ExInfo("RenderUTF8")
00097                 .addInfo("text", text));
00098     }
00099 
00100     //NOTE: at index 0 is bg color
00101     if (SDL_SetColorKey(raw_surface, SDL_SRCCOLORKEY, 0) < 0) {
00102         throw SDLException(ExInfo("SetColorKey"));
00103     }
00104 
00105     SDL_Surface *surface = SDL_DisplayFormat(raw_surface);
00106     if (!surface) {
00107         throw SDLException(ExInfo("DisplayFormat"));
00108     }
00109     SDL_FreeSurface(raw_surface);
00110 
00111     return surface;
00112 }

SDL_Surface * Font::renderTextOutlined const std::string &  text,
const SDL_Color &  color,
int  outlineWidth = 1
const
 

Render text with black outline around font.

Parameters:
text utf-8 encoded text
color text color
outlineWidth outline width
Returns:
new rendered surface

Definition at line 122 of file Font.cpp.

00124 {
00125     SDL_Surface *surface = renderText(" " + text + " ", color);
00126     SDL_Color black = {0, 0, 0, 255};
00127     Outline outline(black, outlineWidth);
00128 
00129     outline.drawOnColorKey(surface);
00130     return surface;
00131 }

void Font::shutdown  )  [static]
 

Deinit font subsystem.

Definition at line 59 of file Font.cpp.

00060 {
00061     TTF_Quit();
00062 }


The documentation for this class was generated from the following files:
Generated on Wed Jun 1 09:55:09 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2