UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
r_font.cpp File Reference

font handling with SDL_ttf font engine More...

#include "r_local.h"
#include "r_font.h"
#include "r_error.h"
#include "../../shared/utf8.h"

Go to the source code of this file.

Data Structures

struct  chunkCache_t
 This structure holds one piece of text (usually a whole line) and the texture on which it is rendered. It also holds positioning information about the place of this piece in a multiline text. Further information is held in the wrapCache_t struct that points to this struct. More...
 
struct  wrapCache_s
 This structure caches information about rendering a text in one font wrapped to a specific width. It points to structures in the chunkCache that cache detailed information and the textures used. More...
 
struct  fontRenderStyle_t
 

Macros

#define MAX_CACHE_STRING   128
 
#define MAX_CHUNK_CACHE   1024 /* making this bigger uses more GL textures */
 
#define MAX_WRAP_CACHE   1024 /* making this bigger uses more memory */
 
#define MAX_WRAP_HASH   4096 /* making this bigger reduces collisions */
 
#define MAX_FONTS   16
 
#define MAX_FONTNAME   32
 
#define MAX_TRUNCMARKER   16 /* enough for 3 chinese chars */
 
#define BUF_SIZE   4096
 
#define NUM_FONT_STYLES   (sizeof(fontStyle) / sizeof(fontRenderStyle_t))
 

Typedefs

typedef struct wrapCache_s wrapCache_t
 This structure caches information about rendering a text in one font wrapped to a specific width. It points to structures in the chunkCache that cache detailed information and the textures used. More...
 

Functions

void R_FontSetTruncationMarker (const char *marker)
 
void R_FontCleanCache (void)
 Clears font cache and frees memory associated with the cache. More...
 
void R_FontShutdown (void)
 frees the SDL_ttf fonts More...
 
static font_tR_FontAnalyze (const char *name, const char *path, int renderStyle, int size)
 
font_tR_GetFont (const char *name)
 Searches the array of available fonts (see fonts.ufo) More...
 
void R_FontListCache_f (void)
 Console command binding to show the font cache. More...
 
static int R_FontHash (const char *string, const font_t *font)
 
static int R_FontChunkLength (const font_t *f, char *text, int len)
 Calculate the width in pixels needed to render a piece of text. Can temporarily modify the caller's string but leaves it unchanged. More...
 
static int R_FontFindFit (const font_t *font, char *text, int maxlen, int maxWidth, int *widthp)
 Find longest part of text that fits in maxWidth pixels, with a clean break such as at a word boundary. Can temporarily modify the caller's string but leaves it unchanged. Assumes whole string won't fit. More...
 
static int R_FontFindTruncFit (const font_t *f, const char *text, int maxlen, int maxWidth, bool mark, int *widthp)
 Find longest part of text that fits in maxWidth pixels, with a marker (ellipsis) at the end to show that part of the text was truncated. Assumes whole string won't fit. More...
 
static int R_FontMakeChunks (const font_t *f, const char *text, int maxWidth, longlines_t method, int *lines, bool *aborted)
 Split text into chunks that fit on one line, and create cache entries for those chunks. More...
 
static wrapCache_tR_FontWrapText (const font_t *f, const char *text, int maxWidth, longlines_t method)
 Wrap text according to provided parameters. Pull wrapping from cache if possible. More...
 
void R_FontTextSize (const char *fontId, const char *text, int maxWidth, longlines_t method, int *width, int *height, int *lines, bool *isTruncated)
 Supply information about the size of the text when it is linewrapped and rendered, without actually rendering it. Any of the output parameters may be nullptr. More...
 
static void R_FontGenerateTexture (const font_t *font, const char *text, chunkCache_t *chunk)
 Renders the text surface and converts to 32bit SDL_Surface that is stored in font_t structure. More...
 
static void R_FontDrawTexture (int texId, int x, int y, int w, int h)
 
int R_FontDrawString (const char *fontId, align_t align, int x, int y, int absX, int maxWidth, int lineHeight, const char *c, int boxHeight, int scrollPos, int *curLine, longlines_t method)
 
void R_FontInit (void)
 
void R_FontRegister (const char *name, int size, const char *path, const char *style)
 

Variables

static int numFonts = 0
 
static font_t fonts [MAX_FONTS]
 
static chunkCache_t chunkCache [MAX_CHUNK_CACHE]
 
static wrapCache_t wrapCache [MAX_WRAP_CACHE]
 
static wrapCache_thash [MAX_WRAP_HASH]
 
static int numChunks = 0
 
static int numWraps = 0
 
static char truncmarker [MAX_TRUNCMARKER] = "..."
 This string is added at the end of truncated strings. By default it is an ellipsis, but the caller can change that. More...
 
static const fontRenderStyle_t fontStyle []
 
static const float font_texcoords []
 

Detailed Description

font handling with SDL_ttf font engine

Definition in file r_font.cpp.

Macro Definition Documentation

#define BUF_SIZE   4096

Definition at line 39 of file r_font.cpp.

Referenced by R_FontFindTruncFit(), R_FontGenerateTexture(), and R_FontMakeChunks().

#define MAX_CACHE_STRING   128

Definition at line 31 of file r_font.cpp.

#define MAX_CHUNK_CACHE   1024 /* making this bigger uses more GL textures */

Definition at line 32 of file r_font.cpp.

Referenced by R_FontListCache_f(), and R_FontMakeChunks().

#define MAX_FONTNAME   32

Definition at line 36 of file r_font.cpp.

#define MAX_FONTS   16

Definition at line 35 of file r_font.cpp.

Referenced by R_FontAnalyze().

#define MAX_TRUNCMARKER   16 /* enough for 3 chinese chars */

Definition at line 37 of file r_font.cpp.

#define MAX_WRAP_CACHE   1024 /* making this bigger uses more memory */

Definition at line 33 of file r_font.cpp.

Referenced by R_FontListCache_f(), and R_FontWrapText().

#define MAX_WRAP_HASH   4096 /* making this bigger reduces collisions */

Definition at line 34 of file r_font.cpp.

Referenced by R_FontHash().

#define NUM_FONT_STYLES   (sizeof(fontStyle) / sizeof(fontRenderStyle_t))

Definition at line 102 of file r_font.cpp.

Referenced by R_FontRegister().

Typedef Documentation

typedef struct wrapCache_s wrapCache_t

This structure caches information about rendering a text in one font wrapped to a specific width. It points to structures in the chunkCache that cache detailed information and the textures used.

Note
Caching text-wrapping information is particularly important for Cyrillic and possibly other non-ascii text, where TTF_SizeUTF8() is almost as slow as rendering. Intro sequence went from 4 fps to 50 after introducing the wrapCache.

Function Documentation

static font_t* R_FontAnalyze ( const char *  name,
const char *  path,
int  renderStyle,
int  size 
)
static
Todo:
Check whether font is already loaded

Definition at line 165 of file r_font.cpp.

References font_s::buffer, Com_Error(), ERR_FATAL, f, font_s::font, FS_LoadFile(), font_s::height, font_s::lineSkip, MAX_FONTS, Mem_Dup, font_s::name, name, numFonts, OBJZERO, font_s::rw, and font_s::style.

Referenced by R_FontRegister().

static int R_FontChunkLength ( const font_t f,
char *  text,
int  len 
)
static

Calculate the width in pixels needed to render a piece of text. Can temporarily modify the caller's string but leaves it unchanged.

Definition at line 268 of file r_font.cpp.

References font_s::font, and len.

Referenced by R_FontFindFit(), and R_FontMakeChunks().

void R_FontCleanCache ( void  )

Clears font cache and frees memory associated with the cache.

Definition at line 121 of file r_font.cpp.

References i, numChunks, numWraps, OBJZERO, and R_CheckError.

Referenced by R_FontMakeChunks(), R_FontShutdown(), R_FontWrapText(), and R_ReinitOpenglContext().

int R_FontDrawString ( const char *  fontId,
align_t  align,
int  x,
int  y,
int  absX,
int  maxWidth,
int  lineHeight,
const char *  c,
int  boxHeight,
int  scrollPos,
int curLine,
longlines_t  method 
)
Parameters
[in]fontIdthe font id (defined in ufos/fonts.ufo)
alignAlignment of the text inside the text zone
[in]xCurrent x position (may differ from absX due to tabs e.g.)
[in]yCurrent y position (may differ from absY due to linebreaks)
[in]absXAbsolute x value for this string
[in]maxWidthMax width - relative from absX
[in]lineHeightThe lineheight of that node
[in]cThe string to draw
boxHeightNumber of line the box can contain. If <= 0 the value is autogenerated according to the number of line of the text input
[in]scrollPosStarting line in this node (due to scrolling)
[in]curLineCurrent line (see lineHeight)
methodExplain the way we manage line overflow
Note
the x, y, width and height values are all normalized here - don't use the viddef settings for drawstring calls - make them all relative to VID_NORM_WIDTH and VID_NORM_HEIGHT
Todo:
This could be replaced with a set of much simpler interfaces.

Definition at line 687 of file r_font.cpp.

References wrapCache_s::chunkIdx, i, chunkCache_t::linenum, wrapCache_s::numChunks, wrapCache_s::numLines, R_FontDrawTexture(), R_FontGenerateTexture(), R_FontWrapText(), R_GetFont(), chunkCache_t::texnum, chunkCache_t::texsize, and chunkCache_t::width.

Referenced by UI_BaseInventoryNodeDraw2(), and UI_DrawString().

static void R_FontDrawTexture ( int  texId,
int  x,
int  y,
int  w,
int  h 
)
static
static int R_FontFindFit ( const font_t font,
char *  text,
int  maxlen,
int  maxWidth,
int widthp 
)
static

Find longest part of text that fits in maxWidth pixels, with a clean break such as at a word boundary. Can temporarily modify the caller's string but leaves it unchanged. Assumes whole string won't fit.

Parameters
[in]fontThe font we are using
[in]textThe text to squeeze into maxWidth
[in]maxlenThe net length of the text (without trailing spaces)
[in]maxWidthThe number of available pixels
[out]widthpPixel width of part that fits.
Returns
String length of part that fits.
Todo:
Smart breaking of Chinese text

Definition at line 294 of file r_font.cpp.

References len, maxlen, R_FontChunkLength(), and UTF8_CONTINUATION_BYTE.

Referenced by R_FontMakeChunks().

static int R_FontFindTruncFit ( const font_t f,
const char *  text,
int  maxlen,
int  maxWidth,
bool  mark,
int widthp 
)
static

Find longest part of text that fits in maxWidth pixels, with a marker (ellipsis) at the end to show that part of the text was truncated. Assumes whole string won't fit.

Definition at line 347 of file r_font.cpp.

References BUF_SIZE, font_s::font, len, maxlen, Q_strncpyz(), truncmarker, and UTF8_CONTINUATION_BYTE.

Referenced by R_FontMakeChunks().

static void R_FontGenerateTexture ( const font_t font,
const char *  text,
chunkCache_t chunk 
)
static

Renders the text surface and converts to 32bit SDL_Surface that is stored in font_t structure.

See also
R_FontCacheGLSurface
TTF_RenderUTF8_Blended
SDL_CreateRGBSurface
SDL_LowerBlit
SDL_FreeSurface

Definition at line 555 of file r_font.cpp.

References BUF_SIZE, Com_Printf(), font_s::font, rconfig_t::gl_alpha_format, rconfig_t::gl_compressed_alpha_format, glGenTextures(), glTexImage2D(), chunkCache_t::len, chunkCache_t::pos, Q_strncpyz(), R_BindTexture, R_CheckError, r_config, chunkCache_t::texnum, chunkCache_t::texsize, chunkCache_t::truncated, truncmarker, Vector2Set, and chunkCache_t::width.

Referenced by R_FontDrawString().

static int R_FontHash ( const char *  string,
const font_t font 
)
static
Parameters
[in]stringString to build the hash value for
[in]fontThe font the string uses
Returns
hash value for given string

Definition at line 254 of file r_font.cpp.

References fonts, i, and MAX_WRAP_HASH.

Referenced by R_FontWrapText().

void R_FontListCache_f ( void  )

Console command binding to show the font cache.

Definition at line 228 of file r_font.cpp.

References Com_Printf(), i, MAX_CHUNK_CACHE, MAX_WRAP_CACHE, wrapCache_s::next, numChunks, numWraps, and wrapCache_s::text.

static int R_FontMakeChunks ( const font_t f,
const char *  text,
int  maxWidth,
longlines_t  method,
int lines,
bool *  aborted 
)
static

Split text into chunks that fit on one line, and create cache entries for those chunks.

Returns
number of chunks allocated in chunkCache.
Todo:
check for infinite recursion here

Definition at line 378 of file r_font.cpp.

References BUF_SIZE, chunkCache_t::len, len, chunkCache_t::linenum, LONGLINES_PRETTYCHOP, LONGLINES_WRAP, MAX_CHUNK_CACHE, numChunks, chunkCache_t::pos, Q_strncpyz(), R_FontChunkLength(), R_FontCleanCache(), R_FontFindFit(), R_FontFindTruncFit(), chunkCache_t::truncated, UTF8_char_len(), UTF8_CONTINUATION_BYTE, and chunkCache_t::width.

Referenced by R_FontWrapText().

void R_FontRegister ( const char *  name,
int  size,
const char *  path,
const char *  style 
)

Definition at line 750 of file r_font.cpp.

References i, NUM_FONT_STYLES, Q_strcasecmp, R_FontAnalyze(), and fontRenderStyle_t::renderStyle.

Referenced by UI_RegisterFont().

void R_FontSetTruncationMarker ( const char *  marker)

Definition at line 112 of file r_font.cpp.

References Q_strncpyz(), and truncmarker.

Referenced by CL_NewLanguage().

void R_FontShutdown ( void  )

frees the SDL_ttf fonts

See also
R_FontCleanCache

Definition at line 144 of file r_font.cpp.

References i, Mem_Free, numFonts, OBJZERO, and R_FontCleanCache().

Referenced by CL_NewLanguage(), R_Shutdown(), and UI_Restart_f().

void R_FontTextSize ( const char *  fontId,
const char *  text,
int  maxWidth,
longlines_t  method,
int width,
int height,
int lines,
bool *  isTruncated 
)

Supply information about the size of the text when it is linewrapped and rendered, without actually rendering it. Any of the output parameters may be nullptr.

Parameters
[in]fontIdthe font id (defined in ufos/fonts.ufo)
[in]textThe text to check
maxWidthMax width available
methodLine overflow method
[out]widthreceives width in pixels of the longest line in the text
[out]heightreceives height in pixels when rendered with standard line height
[out]linesreceives total number of lines in text, including blank ones
[out]isTruncatedreceives true, if the text must be trucated

Definition at line 524 of file r_font.cpp.

References wrapCache_s::chunkIdx, font_s::height, i, font_s::lineSkip, wrapCache_s::numChunks, wrapCache_s::numLines, R_FontWrapText(), R_GetFont(), chunkCache_t::truncated, and chunkCache_t::width.

Referenced by uiButtonNode::draw(), uiTabNode::draw(), uiText2Node::drawText(), uiTextNode::drawText(), uiStringNode::drawTooltip(), HUD_UpdateCursor(), UI_BaseInventoryNodeDrawItems(), UI_BaseInventoryNodeGetItem(), UI_DrawNotice(), UI_DrawTooltip(), UI_MessageGetLines(), and UI_TabNodeTabAtPosition().

static wrapCache_t* R_FontWrapText ( const font_t f,
const char *  text,
int  maxWidth,
longlines_t  method 
)
static
font_t* R_GetFont ( const char *  name)

Searches the array of available fonts (see fonts.ufo)

Definition at line 210 of file r_font.cpp.

References Com_Error(), Com_Printf(), ERR_FATAL, i, numFonts, and Q_streq.

Referenced by R_FontDrawString(), R_FontTextSize(), and UI_FontGetHeight().

Variable Documentation

chunkCache_t chunkCache[MAX_CHUNK_CACHE]
static

Definition at line 84 of file r_font.cpp.

const float font_texcoords[]
static
Initial value:
= {
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0
}

Definition at line 633 of file r_font.cpp.

Referenced by R_FontDrawTexture().

font_t fonts[MAX_FONTS]
static

Definition at line 82 of file r_font.cpp.

Referenced by R_FontHash().

const fontRenderStyle_t fontStyle[]
static
Initial value:
= {
{"TTF_STYLE_NORMAL", TTF_STYLE_NORMAL},
{"TTF_STYLE_BOLD", TTF_STYLE_BOLD},
{"TTF_STYLE_ITALIC", TTF_STYLE_ITALIC},
{"TTF_STYLE_UNDERLINE", TTF_STYLE_UNDERLINE}
}

Definition at line 103 of file r_font.cpp.

int numChunks = 0
static
int numFonts = 0
static

Definition at line 81 of file r_font.cpp.

Referenced by R_FontAnalyze(), R_FontInit(), R_FontShutdown(), and R_GetFont().

int numWraps = 0
static

Definition at line 88 of file r_font.cpp.

Referenced by R_FontCleanCache(), R_FontInit(), R_FontListCache_f(), and R_FontWrapText().

char truncmarker[MAX_TRUNCMARKER] = "..."
static

This string is added at the end of truncated strings. By default it is an ellipsis, but the caller can change that.

See also
R_FontSetTruncationMarker

Definition at line 95 of file r_font.cpp.

Referenced by R_FontFindTruncFit(), R_FontGenerateTexture(), and R_FontSetTruncationMarker().

wrapCache_t wrapCache[MAX_WRAP_CACHE]
static

Definition at line 85 of file r_font.cpp.