Atrinik Client  4.0
Macros | Functions | Variables
text.c File Reference
#include <global.h>
#include <toolkit/string.h>

Go to the source code of this file.

Macros

#define TEXT_SHOW_SELECT_BEGIN()
 
#define TEXT_SHOW_SELECT_END()
 

Functions

static char * font_get_hash_key (const char *name, uint8_t size, char *buf, size_t buf_size)
 
static TTF_Font * font_open (const char *name, uint8_t size)
 
static font_structfont_new (const char *name, uint8_t size)
 
font_structfont_get_weak (const char *name, uint8_t size)
 
font_structfont_get (const char *name, uint8_t size)
 
font_structfont_get_size (font_struct *font, int8_t size)
 
void font_free (font_struct *font)
 
void font_gc (void)
 
void text_init (void)
 
void text_deinit (void)
 
void text_offset_set (int x, int y)
 
void text_offset_reset (void)
 
void text_color_set (int r, int g, int b)
 
void text_set_selection (int64_t *start, int64_t *end, uint8_t *started)
 
void text_set_anchor_handle (text_anchor_handle_func func)
 
void text_set_anchor_info (void *ptr)
 
char * text_strip_markup (char *buf, size_t *buf_len, uint8_t do_free)
 
char * text_escape_markup (const char *buf)
 
static int text_adjust_coords (SDL_Surface *surface, int *mx, int *my)
 
int text_color_parse (const char *color_notation, SDL_Color *color)
 
void text_anchor_execute (text_info_struct *info, void *custom_data)
 
void text_show_character_init (text_info_struct *info)
 
int text_show_character (font_struct **font, font_struct *orig_font, SDL_Surface *surface, SDL_Rect *dest, const char *cp, SDL_Color *color, SDL_Color *orig_color, uint64_t flags, SDL_Rect *box, int *x_adjust, text_info_struct *info)
 
int glyph_get_width (font_struct *font, char c)
 
int glyph_get_height (font_struct *font, char c)
 
void text_show (SDL_Surface *surface, font_struct *font, const char *text, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box)
 
void text_show_shadow (SDL_Surface *surface, font_struct *font, const char *text, int x, int y, const char *color_notation, const char *color_shadow_notation, uint64_t flags, SDL_Rect *box)
 
void text_show_format (SDL_Surface *surface, font_struct *font, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box, const char *format,...)
 
void text_show_shadow_format (SDL_Surface *surface, font_struct *font, int x, int y, const char *color_notation, const char *color_shadow_notation, uint64_t flags, SDL_Rect *box, const char *format,...)
 
int text_get_width (font_struct *font, const char *text, uint64_t flags)
 
int text_get_height (font_struct *font, const char *text, uint64_t flags)
 
void text_get_width_height (font_struct *font, const char *text, uint64_t flags, SDL_Rect *box, uint16_t *w, uint16_t *h)
 
void text_truncate_overflow (font_struct *font, char *text, int max_width)
 
void text_anchor_parse (text_info_struct *info, const char *text)
 
void text_enable_debug (void)
 

Variables

static uint8_t text_debug = 0
 
static uint8_t text_anchor_help_clicked = 0
 
static char text_anchor_help [HUGE_BUF]
 
static int text_offset_mx = -1
 
static int text_offset_my = -1
 
static int64_t * selection_start = NULL
 
static int64_t * selection_end = NULL
 
static uint8_t * selection_started = NULL
 
static SDL_Color text_link_color_default = {96, 160, 255, 0}
 
static SDL_Color text_link_color = {0, 0, 0, 0}
 
static text_anchor_handle_func text_anchor_handle = NULL
 
static void * text_anchor_info_ptr = NULL
 
static font_structfonts
 

Detailed Description

Text drawing API. Uses SDL_ttf for rendering.

Author
Alex Tokar

Definition in file text.c.

Macro Definition Documentation

#define TEXT_SHOW_SELECT_BEGIN ( )

Begin handling text mouse-based selection.

Definition at line 1901 of file text.c.

#define TEXT_SHOW_SELECT_END ( )
Value:
{ \
if (select_color_changed) \
{ \
color = select_color_orig; \
select_color_changed = 0; \
} \
if (selection_start && selection_end && mstate == SDL_BUTTON_LEFT) \
{ \
if (my >= dest.y && my <= dest.y + FONT_HEIGHT(FONT_TRY_INFO(font, info, surface)) && mx >= old_x && mx <= old_x + glyph_get_width(FONT_TRY_INFO(font, info, surface), *cp)) \
{ \
{ \
*selection_end = cp - text; \
} \
else \
{ \
*selection_start = cp - text; \
} \
} \
} \
}
static int64_t * selection_start
Definition: text.c:59
#define FONT_HEIGHT(font)
Definition: text.h:327
static uint8_t * selection_started
Definition: text.c:63
int glyph_get_width(font_struct *font, char c)
Definition: text.c:1855
static int64_t * selection_end
Definition: text.c:61

End handling text mouse-based selection.

Definition at line 1938 of file text.c.

Function Documentation

void font_free ( font_struct font)

Free a font.

Parameters
fontFont to free.

Definition at line 262 of file text.c.

void font_gc ( void  )

Garbage-collect fonts.

Definition at line 297 of file text.c.

font_struct* font_get ( const char *  name,
uint8_t  size 
)

Like font_get_weak(), but the returned pointer will have increased reference count, so remember to use font_free() in your cleanup function.

Parameters
nameName of the font to get.
sizeSize of the font to get.
Returns
Font; can be NULL in case of failure.

Definition at line 216 of file text.c.

static char* font_get_hash_key ( const char *  name,
uint8_t  size,
char *  buf,
size_t  buf_size 
)
static

Get a hash table key for a font.

Parameters
nameName of the font.
sizeSize of the font.
bufWhere to store the result.
buf_sizeSize of 'buf'.
Returns
'buf'.

Definition at line 98 of file text.c.

font_struct* font_get_size ( font_struct font,
int8_t  size 
)

Acquire a weak reference to a font of a larger or smaller size.

Parameters
fontFont.
sizeSize adjustment; 1 for a bigger one, -1 for a smaller one.
Returns
The font. NULL if the size is not in an acceptable range or some other failure occurs.

Definition at line 242 of file text.c.

font_struct* font_get_weak ( const char *  name,
uint8_t  size 
)

Acquires a weak reference to the font of the specified name and size. Do NOT store a reference to this pointer without explicit FONT_INCREF. If in doubt, use font_get() instead.

This function will attempt to find the specified font with the specified size in a hash table, and, failing that, it will attempt to initialized such font.

Parameters
nameName of the font to get.
sizeSize of the font to get.
Returns
Font; can be NULL in case of failure.

Definition at line 186 of file text.c.

static font_struct* font_new ( const char *  name,
uint8_t  size 
)
static

Allocate a new font structure.

Parameters
nameName of the font.
sizeSize of the font.
Returns
The allocated font; NULL on failure.

Definition at line 145 of file text.c.

static TTF_Font* font_open ( const char *  name,
uint8_t  size 
)
static

Attempt to open a TTF font. Will attempt various paths/extensions until giving up.

Parameters
nameName of the font.
sizeSize of the font.
Returns
Opened font on success, NULL on failure.

Definition at line 115 of file text.c.

int glyph_get_height ( font_struct font,
char  c 
)

Get glyph's height.

Parameters
fontFont of the glyph.
cThe glyph.
Returns
The height.

Definition at line 1883 of file text.c.

int glyph_get_width ( font_struct font,
char  c 
)

Get glyph's width.

Parameters
fontFont of the glyph.
cThe glyph.
Returns
The width.

Definition at line 1855 of file text.c.

static int text_adjust_coords ( SDL_Surface *  surface,
int *  mx,
int *  my 
)
static

Adjust mouse X/Y coordinates for mouse-related checks based on which surface we're using.

Parameters
surfaceThe surface.
[out]mxMouse X, may be modified.
[out]myMouse Y, may be modified.
Returns
1 if mouse-related checks can happen, 0 otherwise.

Definition at line 563 of file text.c.

void text_anchor_execute ( text_info_struct info,
void *  custom_data 
)

Execute anchor.

Parameters
infoText info, should contain the anchor action and tag position.
custom_dataUser-supplied data. Can be NULL.

Definition at line 632 of file text.c.

void text_anchor_parse ( text_info_struct info,
const char *  text 
)

Utility function to parse text and store information about anchor tag, if any.

Parameters
infoWhere to store the information.
textThe text to parse.

Definition at line 2471 of file text.c.

int text_color_parse ( const char *  color_notation,
SDL_Color *  color 
)

Parse the given string as a HTML notation color, and store the RGB values in 'color'.

Parameters
color_notationThe HTML notation to parse.
colorWhere the RGB values will be stored.
Returns
1 if the notation was parsed successfully, 0 otherwise.

Definition at line 603 of file text.c.

void text_color_set ( int  r,
int  g,
int  b 
)

Set color to use for links. Will be reset to default color after the next call to string rendering is done.

Parameters
rRed.
gGreen.
bBlue.

Definition at line 406 of file text.c.

void text_deinit ( void  )

Deinitialize the text API.

Definition at line 347 of file text.c.

void text_enable_debug ( void  )

Enable text debugging.

Definition at line 2494 of file text.c.

char* text_escape_markup ( const char *  buf)

Escapes markup in 'buf'.

Parameters
bufString to escape markup in.
Returns
New string with markup escaped; never NULL. Must be freed.

Definition at line 532 of file text.c.

int text_get_height ( font_struct font,
const char *  text,
uint64_t  flags 
)

Calculate string's pixel height, taking into account markup, if applicable.

It is usually enough to use FONT_HEIGHT() to get the string's font height, unless markup is allowed, in which case the maximum used height might be different.

Parameters
fontFont. One of FONT_xxx.
textString to get height of.
flagsOne or a combination of Text flags.
Returns
The string's height.

Definition at line 2364 of file text.c.

int text_get_width ( font_struct font,
const char *  text,
uint64_t  flags 
)

Calculate string's pixel width, taking into account markup, if applicable.

Parameters
fontFont. One of FONT_xxx.
textString to get width of.
flagsOne or a combination of Text flags.
Returns
The string's width.

Definition at line 2328 of file text.c.

void text_get_width_height ( font_struct font,
const char *  text,
uint64_t  flags,
SDL_Rect *  box,
uint16_t *  w,
uint16_t *  h 
)

Calculate text's pixel height and width.

Parameters
fontFont. One of FONT_xxx.
textText to get width and height of.
flagsCombination of Text flags flags.
boxOptional, may contain maximum width/height (for word wrap, for example).
[out]wWill contain the calculated width.
[out]hWill contain the calculated height.

Definition at line 2410 of file text.c.

void text_init ( void  )

Initialize the text API. Should only be done once.

Definition at line 336 of file text.c.

void text_offset_reset ( void  )

Reset the text offset. This must be done after text_offset_set() and text_show() calls eventually,

Definition at line 391 of file text.c.

void text_offset_set ( int  x,
int  y 
)

If text_show() is called on surface that is not ScreenSurface, you must use this to set mouse X/Y detection offset, so things like links will work correctly.

Note that this is not required for widget surfaces, as it's done automatically by searching the widgets for the surface that is being used.

Parameters
xX position of the surface.
yY position of the surface.

Definition at line 381 of file text.c.

void text_set_anchor_handle ( text_anchor_handle_func  func)

Set the anchor handler function.

Parameters
funcThe function.

Definition at line 437 of file text.c.

void text_set_anchor_info ( void *  ptr)

Set the value of text_anchor_info_ptr.

Parameters
ptrValue to set.

Definition at line 447 of file text.c.

void text_set_selection ( int64_t *  start,
int64_t *  end,
uint8_t *  started 
)

Allow grabbing a text selection.

Parameters
startPointer that will be used to store start of selection.
endPointer that will be used to store start of selection.
startedPointer that is used to determine whether to store selection data in start or end.
Note
You must call this with all arguments set to NULL after your call to string drawing routines.

Definition at line 425 of file text.c.

void text_show ( SDL_Surface *  surface,
font_struct font,
const char *  text,
int  x,
int  y,
const char *  color_notation,
uint64_t  flags,
SDL_Rect *  box 
)

Draw a string on the specified surface.

Parameters
surfaceSurface to draw on.
fontFont to use. One of FONT_xxx.
textThe string to draw.
xX position.
yY position.
color_notationColor to use.
flagsOne or a combination of Text flags.
boxIf word wrap was enabled by passing TEXT_WORD_WRAP as one of the 'flags', this is used to get the max width from. Also even if word wrap is disabled, this is used to get the max height from, if set (both box->w and box->h can be 0 to indicate unlimited).

Definition at line 1983 of file text.c.

int text_show_character ( font_struct **  font,
font_struct orig_font,
SDL_Surface *  surface,
SDL_Rect *  dest,
const char *  cp,
SDL_Color *  color,
SDL_Color *  orig_color,
uint64_t  flags,
SDL_Rect *  box,
int *  x_adjust,
text_info_struct info 
)

Draw one character on the screen or parse markup (if applicable).

Parameters
[out]fontFont to use. One of FONT_xxx.
orig_fontOriginal font, used for the font tag.
surfaceSurface to draw on. If NULL, there is no drawing done, but the return value is still calculated along with dest->w.
destDestination, will have width (and x, if surface wasn't NULL) updated.
cpString we are working on, cp[0] is the character to draw.
colorColor to use.
orig_colorOriginal color.
flagsFlags as passed to text_show().
Returns
How many characters to jump. Usually 1, but can be more in case of markup tags that need to be jumped over, since they are not actually drawn.

Definition at line 753 of file text.c.

void text_show_character_init ( text_info_struct info)

Initialize the 'info' argument of text_show_character(). Should only be called once.

Parameters
infoThe text information to initialize.

Definition at line 704 of file text.c.

void text_show_format ( SDL_Surface *  surface,
font_struct font,
int  x,
int  y,
const char *  color_notation,
uint64_t  flags,
SDL_Rect *  box,
const char *  format,
  ... 
)

Like text_show(), but allows using printf-like format specifiers.

Draw a string on the specified surface.

Parameters
surfaceSurface to draw on.
fontFont to use. One of FONT_xxx.
textThe string to draw.
xX position.
yY position.
color_notationColor to use.
flagsOne or a combination of Text flags.
boxIf word wrap was enabled by passing TEXT_WORD_WRAP as one of the 'flags', this is used to get the max width from. Also even if word wrap is disabled, this is used to get the max height from, if set (both box->w and box->h can be 0 to indicate unlimited).

Definition at line 2289 of file text.c.

void text_show_shadow ( SDL_Surface *  surface,
font_struct font,
const char *  text,
int  x,
int  y,
const char *  color_notation,
const char *  color_shadow_notation,
uint64_t  flags,
SDL_Rect *  box 
)

Draw a string with a shadow.

Parameters
surfaceSurface to draw on.
fontFont to use. One of FONT_xxx.
textThe string to draw.
xX position.
yY position.
color_notationColor to use.
color_shadow_notationColor to use for the shadow.
flagsOne or a combination of Text flags.
boxIf word wrap was enabled by passing TEXT_WORD_WRAP as one of the 'flags', this is used to get the max width from. Also even if word wrap is disabled, this is used to get the max height from, if set (both box->w and box->h can be 0 to indicate unlimited).

Definition at line 2278 of file text.c.

void text_show_shadow_format ( SDL_Surface *  surface,
font_struct font,
int  x,
int  y,
const char *  color_notation,
const char *  color_shadow_notation,
uint64_t  flags,
SDL_Rect *  box,
const char *  format,
  ... 
)

Like text_show_shadow(), but allows using printf-like format specifiers.

Draw a string with a shadow.

Parameters
surfaceSurface to draw on.
fontFont to use. One of FONT_xxx.
textThe string to draw.
xX position.
yY position.
color_notationColor to use.
color_shadow_notationColor to use for the shadow.
flagsOne or a combination of Text flags.
boxIf word wrap was enabled by passing TEXT_WORD_WRAP as one of the 'flags', this is used to get the max width from. Also even if word wrap is disabled, this is used to get the max height from, if set (both box->w and box->h can be 0 to indicate unlimited).

Definition at line 2305 of file text.c.

char* text_strip_markup ( char *  buf,
size_t *  buf_len,
uint8_t  do_free 
)

Remove all markup tags, including their contents.

Entities will also be replaced with their proper replacements.

Parameters
bufBuffer containing the text with markup, from which to remove tags.
[out]lenLength of 'buf'. This will contain the length of the new string, without markup tags. Can be NULL, in which case length of the original string will be calculated automatically.
do_freeIf 1, will automatically free 'buf'.
Returns
Newly allocated string with markup removed, and entities replaced.

Definition at line 468 of file text.c.

void text_truncate_overflow ( font_struct font,
char *  text,
int  max_width 
)

Truncate a text string if overflow would occur when rendering it.

Parameters
fontFont used for the text.
textThe text.
max_widthMaximum possible width.

Definition at line 2446 of file text.c.

Variable Documentation

font_struct* fonts
static

The usable fonts.

Definition at line 83 of file text.c.

int64_t* selection_end = NULL
static

Pointer to integer holding the selection end.

Definition at line 61 of file text.c.

int64_t* selection_start = NULL
static

Pointer to integer holding the selection start.

Definition at line 59 of file text.c.

uint8_t* selection_started = NULL
static

If 1, selection start has been set, and the end should be updated next.

Definition at line 63 of file text.c.

text_anchor_handle_func text_anchor_handle = NULL
static

Anchor handler function to try and execute before the defaults.

Parameters
anchor_actionThe action to execute, which can be empty (but not NULL) - for example, 'help'.
bufText to pass to the action decided by the anchor action; in case of links, the URL to open, for example. Will not contain any markup, and should not be freed.
lenLength of 'buf'.
custom_dataUser-supplied data. Can be NULL.
Returns
1 if handled the action and should not handle it using default actions, 0 otherwise.

Definition at line 71 of file text.c.

char text_anchor_help[HUGE_BUF]
static

Help GUI to open if text_anchor_help_clicked is 1.

Definition at line 51 of file text.c.

uint8_t text_anchor_help_clicked = 0
static

If not 0, text_anchor_help is used to open the help GUI after finishing drawing.

This is used so the text being drawn is not changed in the middle of the drawing by clicking on a link.

Definition at line 48 of file text.c.

void* text_anchor_info_ptr = NULL
static

If non-NULL, when blitting text inside an anchor tag, anchor execution function will be called with this pointer as the optional custom data argument.

Definition at line 78 of file text.c.

uint8_t text_debug = 0
static

If 1, all text shown using 'box' parameter of text_show() for max width/height will have a frame around it.

Definition at line 39 of file text.c.

SDL_Color text_link_color = {0, 0, 0, 0}
static

Current text link color.

Definition at line 68 of file text.c.

SDL_Color text_link_color_default = {96, 160, 255, 0}
static

Default link color.

Definition at line 66 of file text.c.

int text_offset_mx = -1
static

Mouse X offset.

Definition at line 54 of file text.c.

int text_offset_my = -1
static

Mouse Y offset.

Definition at line 56 of file text.c.