UFO: Alien Invasion
Loading...
Searching...
No Matches
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"
Include dependency graph for r_font.cpp:

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_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...
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))

Functions

void R_FontSetTruncationMarker (const char *marker)
void R_FontCleanCache (void)
 Clears font cache and frees memory associated with the cache.
void R_FontShutdown (void)
 frees the SDL_ttf fonts
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).
void R_FontListCache_f (void)
 Console command binding to show the font cache.
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.
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.
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.
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.
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.
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.
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.
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.
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

◆ BUF_SIZE

#define BUF_SIZE   4096

Definition at line 39 of file r_font.cpp.

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

◆ MAX_CACHE_STRING

#define MAX_CACHE_STRING   128

Definition at line 31 of file r_font.cpp.

◆ MAX_CHUNK_CACHE

#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().

◆ MAX_FONTNAME

#define MAX_FONTNAME   32

Definition at line 36 of file r_font.cpp.

◆ MAX_FONTS

#define MAX_FONTS   16

Definition at line 35 of file r_font.cpp.

Referenced by R_FontAnalyze(), and UI_ParseFont().

◆ MAX_TRUNCMARKER

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

Definition at line 37 of file r_font.cpp.

◆ MAX_WRAP_CACHE

#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().

◆ MAX_WRAP_HASH

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

Definition at line 34 of file r_font.cpp.

Referenced by R_FontHash().

◆ NUM_FONT_STYLES

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

Definition at line 102 of file r_font.cpp.

Referenced by R_FontRegister().

Function Documentation

◆ R_FontAnalyze()

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 Com_Error(), ERR_FATAL, f, fonts, FS_LoadFile(), MAX_FONTS, Mem_Dup, name, numFonts, and OBJZERO.

Referenced by R_FontRegister().

◆ R_FontChunkLength()

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 266 of file r_font.cpp.

References f, and len.

Referenced by R_FontFindFit(), and R_FontMakeChunks().

◆ R_FontCleanCache()

void R_FontCleanCache ( void )

Clears font cache and frees memory associated with the cache.

Definition at line 121 of file r_font.cpp.

References chunkCache, hash, i, numChunks, numWraps, OBJZERO, R_CheckError, and wrapCache.

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

◆ R_FontDrawString()

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 681 of file r_font.cpp.

References chunkCache, wrapCache_t::chunkIdx, i, chunkCache_t::linenum, wrapCache_t::numChunks, wrapCache_t::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().

◆ R_FontDrawTexture()

void R_FontDrawTexture ( int texId,
int x,
int y,
int w,
int h )
static

Definition at line 631 of file r_font.cpp.

References font_texcoords, R_BindTexture, r_state, refdef, texunit_diffuse, and viddef.

Referenced by R_FontDrawString().

◆ R_FontFindFit()

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 292 of file r_font.cpp.

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

Referenced by R_FontMakeChunks().

◆ R_FontFindTruncFit()

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 345 of file r_font.cpp.

References BUF_SIZE, f, len, maxlen, Q_strncpyz(), truncmarker, and UTF8_CONTINUATION_BYTE.

Referenced by R_FontMakeChunks().

◆ R_FontGenerateTexture()

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 553 of file r_font.cpp.

References BUF_SIZE, Com_Printf(), font_t::font, 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().

◆ R_FontHash()

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 252 of file r_font.cpp.

References fonts, i, and MAX_WRAP_HASH.

Referenced by R_FontWrapText().

◆ R_FontInit()

◆ R_FontListCache_f()

void R_FontListCache_f ( void )

Console command binding to show the font cache.

Definition at line 226 of file r_font.cpp.

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

◆ R_FontMakeChunks()

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 376 of file r_font.cpp.

References BUF_SIZE, chunkCache, f, len, LONGLINES_PRETTYCHOP, LONGLINES_WRAP, MAX_CHUNK_CACHE, numChunks, Q_strncpyz(), R_FontChunkLength(), R_FontCleanCache(), R_FontFindFit(), R_FontFindTruncFit(), R_FontMakeChunks(), UTF8_char_len(), and UTF8_CONTINUATION_BYTE.

Referenced by R_FontMakeChunks(), and R_FontWrapText().

◆ R_FontRegister()

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

Definition at line 744 of file r_font.cpp.

References fontStyle, i, name, NUM_FONT_STYLES, Q_strcasecmp, and R_FontAnalyze().

Referenced by UI_RegisterFont().

◆ R_FontSetTruncationMarker()

void R_FontSetTruncationMarker ( const char * marker)

Definition at line 112 of file r_font.cpp.

References Q_strncpyz(), and truncmarker.

Referenced by CL_NewLanguage().

◆ R_FontShutdown()

void R_FontShutdown ( void )

frees the SDL_ttf fonts

See also
R_FontCleanCache

Definition at line 144 of file r_font.cpp.

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

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

◆ R_FontTextSize()

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 522 of file r_font.cpp.

References chunkCache, wrapCache_t::chunkIdx, font_t::height, i, font_t::lineSkip, wrapCache_t::numChunks, wrapCache_t::numLines, R_FontWrapText(), and R_GetFont().

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().

◆ R_FontWrapText()

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

◆ R_GetFont()

font_t * R_GetFont ( const char * name)

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

Definition at line 208 of file r_font.cpp.

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

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

Variable Documentation

◆ chunkCache

◆ font_texcoords

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 627 of file r_font.cpp.

Referenced by R_FontDrawTexture().

◆ fonts

◆ fontStyle

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.

Referenced by R_FontRegister().

◆ hash

◆ numChunks

int numChunks = 0
static

◆ numFonts

◆ numWraps

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().

◆ truncmarker

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

wrapCache_t wrapCache[MAX_WRAP_CACHE]
static

Definition at line 85 of file r_font.cpp.

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