r_font.c 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.c:

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

Defines

#define MAX_CACHE_STRING   128
#define MAX_CHUNK_CACHE   1024
#define MAX_WRAP_CACHE   1024
#define MAX_WRAP_HASH   4096
#define MAX_FONTS   16
#define MAX_FONTNAME   32
#define MAX_TRUNCMARKER   16
#define BUF_SIZE   2048
#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.

Functions

void R_FontSetTruncationMarker (const char *marker)
static 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)
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 *f, 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, qboolean 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, qboolean *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, qboolean *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 NULL.
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.c.


Define Documentation

#define BUF_SIZE   2048

Definition at line 39 of file r_font.c.

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

#define MAX_CACHE_STRING   128

Definition at line 31 of file r_font.c.

#define MAX_CHUNK_CACHE   1024

Definition at line 32 of file r_font.c.

Referenced by R_FontListCache_f(), and R_FontMakeChunks().

#define MAX_FONTNAME   32

Definition at line 36 of file r_font.c.

#define MAX_FONTS   16

Definition at line 35 of file r_font.c.

Referenced by R_FontAnalyze(), and UI_ParseFont().

#define MAX_TRUNCMARKER   16

Definition at line 37 of file r_font.c.

#define MAX_WRAP_CACHE   1024

Definition at line 33 of file r_font.c.

Referenced by R_FontListCache_f(), and R_FontWrapText().

#define MAX_WRAP_HASH   4096

Definition at line 34 of file r_font.c.

Referenced by R_FontHash().

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

Definition at line 102 of file r_font.c.

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 169 of file r_font.c.

References font_s::buffer, Com_Error(), ERR_FATAL, f, font_s::font, FS_LoadFile(), font_s::height, font_s::lineSkip, MAX_FONTS, font_s::name, numFonts, 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 266 of file r_font.c.

References font_s::font.

Referenced by R_FontFindFit(), and R_FontMakeChunks().

static void R_FontCleanCache ( void   )  [static]

Clears font cache and frees memory associated with the cache.

Definition at line 121 of file r_font.c.

References i, numChunks, numWraps, and R_CheckError.

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

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] fontId the font id (defined in ufos/fonts.ufo)
[in] x Current x position (may differ from absX due to tabs e.g.)
[in] y Current y position (may differ from absY due to linebreaks)
[in] absX Absolute x value for this string
[in] absY Absolute y value for this string
[in] maxWidth Max width - relative from absX
[in] lineHeight The lineheight of that node
[in] c The string to draw
[in] scrollPos Starting line in this node (due to scrolling)
[in] curLine Current line (see lineHeight)
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 675 of file r_font.c.

References chunk(), 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 f,
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:
[out] widthp Pixel width of part that fits.
Returns:
String length of part that fits.

Todo:
Smart breaking of Chinese text

Definition at line 290 of file r_font.c.

References len, 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,
qboolean  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.c.

References BUF_SIZE, font_s::font, len, 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.c.

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

Referenced by R_FontDrawString().

static int R_FontHash ( const char *  string  )  [static]
Parameters:
[in] string String to build the hash value for
Returns:
hash value for given string

Definition at line 250 of file r_font.c.

References i, and MAX_WRAP_HASH.

Referenced by R_FontWrapText().

void R_FontInit ( void   ) 

Definition at line 713 of file r_font.c.

References Com_Error(), Com_Printf(), ERR_FATAL, numChunks, numFonts, and numWraps.

Referenced by CL_NewLanguage(), and R_Init().

void R_FontListCache_f ( void   ) 

Console command binding to show the font cache.

Definition at line 223 of file r_font.c.

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,
qboolean 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.c.

References BUF_SIZE, chunkCache_t::len, len, chunkCache_t::linenum, LONGLINES_PRETTYCHOP, LONGLINES_WRAP, MAX_CHUNK_CACHE, numChunks, chunkCache_t::pos, pos, Q_strncpyz(), qfalse, qtrue, 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 741 of file r_font.c.

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.c.

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 146 of file r_font.c.

References FS_FreeFile(), i, numFonts, and R_FontCleanCache().

Referenced by CL_NewLanguage(), and R_Shutdown().

void R_FontTextSize ( const char *  fontId,
const char *  text,
int  maxWidth,
longlines_t  method,
int *  width,
int *  height,
int *  lines,
qboolean 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 NULL.

Parameters:
[out] width receives width in pixels of the longest line in the text
[out] height receives height in pixels when rendered with standard line height
[out] lines receives total number of lines in text, including blank ones

Definition at line 523 of file r_font.c.

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 HUD_DrawMouseCursorText(), HUD_UpdateCursor(), UI_BaseInventoryNodeDrawItems(), UI_BaseInventoryNodeGetItem(), UI_DrawNotice(), UI_DrawTooltip(), UI_MessageGetLines(), UI_StringNodeDrawTooltip(), UI_TabNodeDraw(), UI_TabNodeTabAtPosition(), and UI_TextNodeDrawText().

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 208 of file r_font.c.

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

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.c.

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 626 of file r_font.c.

Referenced by R_FontDrawTexture().

font_t fonts[MAX_FONTS] [static]

Definition at line 82 of file r_font.c.

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.c.

wrapCache_t* hash[MAX_WRAP_HASH] [static]

Definition at line 86 of file r_font.c.

int numChunks = 0 [static]
int numFonts = 0 [static]

Definition at line 81 of file r_font.c.

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

int numWraps = 0 [static]

Definition at line 88 of file r_font.c.

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.c.

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

wrapCache_t wrapCache[MAX_WRAP_CACHE] [static]

Definition at line 85 of file r_font.c.


Generated by  doxygen 1.6.2