naev 0.12.5
font.c File Reference

OpenGL font rendering routines. More...

#include "font.h"
#include "array.h"
#include "distance_field.h"
#include "log.h"
#include "ndata.h"
#include "ntracing.h"
#include "utf8.h"
Include dependency graph for font.c:

Go to the source code of this file.

Data Structures

struct  glFontRow
 Stores the row information for a font. More...
struct  glFontTex
 Stores a texture stash for fonts. More...
struct  glFontGlyph
 Represents a character in the font. More...
struct  font_char_t
 Stores a font character. More...
struct  glFontFile
 Stores a font file. May be referenced by multiple glFonts for size or fallback reasons. More...
struct  glFontStashFreetype
 Freetype Font structure. More...
struct  glFontStash
 Font structure. More...
struct  _linepos_t

Macros

#define MAX_EFFECT_RADIUS    4
#define FONT_DISTANCE_FIELD_SIZE   55
#define HASH_LUT_SIZE   512
#define DEFAULT_TEXTURE_SIZE    1024
#define MAX_ROWS   64

Functions

static int gl_fontstashAddFallback (glFontStash *stsh, const char *fname, unsigned int h)
 Adds a fallback font to a stash.
static size_t font_limitSize (glFontStash *stsh, int *width, const char *text, const int max)
 Limits the text to max.
static const glColour * gl_fontGetColour (uint32_t ch)
 Gets the colour from a character.
static uint32_t font_nextChar (const char *s, size_t *i)
 Reads the next utf-8 sequence out of a string, updating an index. Skips font markup directives.
static glFontGlyphgl_fontGetGlyph (glFontStash *stsh, uint32_t ch)
 Gets or caches a glyph to render.
static void gl_fontRenderStart (const glFontStash *stsh, double x, double y, const glColour *c, double outlineR)
 Starts the rendering engine.
static void gl_fontRenderStartH (const glFontStash *stsh, const mat4 *H, const glColour *c, double outlineR)
static int gl_fontRenderGlyph (glFontStash *stsh, uint32_t ch, const glColour *c, int state)
 Renders a character.
static void gl_fontRenderEnd (void)
 Ends the rendering engine.
static void gl_fontKernStart (void)
 Call at the start of a string/line.
static int gl_fontKernGlyph (glFontStash *stsh, uint32_t ch, glFontGlyph *glyph)
 Return the signed advance (same units as adv_x) ahead of the current char.
static void gl_fontstashftDestroy (glFontStashFreetype *ft)
 Frees resources referenced by a glFontStashFreetype struct.
static glFontStashgl_fontGetStash (const glFont *font)
 Gets the font stash corresponding to a font.
static int gl_fontAddGlyphTex (glFontStash *stsh, font_char_t *ch, glFontGlyph *glyph)
 Adds a font glyph to the texture stash.
void gl_printRestoreClear (void)
 Clears the restoration.
void gl_printRestoreLast (void)
 Restores last colour.
void gl_printRestoreInit (glFontRestore *restore)
 Initializes a restore structure.
void gl_printRestore (const glFontRestore *restore)
 Restores last colour from a restore structure.
void gl_printStoreMax (glFontRestore *restore, const char *text, int max)
 Stores the colour information from a piece of text limited to max characters.
void gl_printStore (glFontRestore *restore, const char *text)
 Stores the colour information from a piece of text.
void gl_printLineIteratorInit (glPrintLineIterator *iter, const glFont *ft_font, const char *text, int width)
 Initialize an iterator object for breaking text into lines.
int gl_printLineIteratorNext (glPrintLineIterator *iter)
 Updates iter with the next line's information.
void gl_printRaw (const glFont *ft_font, double x, double y, const glColour *c, double outlineR, const char *text)
 Prints text on screen.
void gl_printRawH (const glFont *ft_font, const mat4 *H, const glColour *c, const double outlineR, const char *text)
 Prints text on screen using a transformation matrix.
void gl_printMarkerRaw (const glFont *ft_font, double x, double y, const glColour *c, const char *text)
 Wrapper for gl_printRaw for map overlay markers.
void gl_print (const glFont *ft_font, const double x, const double y, const glColour *c, const char *fmt,...)
 Prints text on screen like printf.
int gl_printMaxRaw (const glFont *ft_font, const int max, double x, double y, const glColour *c, double outlineR, const char *text)
 Behaves like gl_printRaw but stops displaying text after a certain distance.
int gl_printMax (const glFont *ft_font, const int max, double x, double y, const glColour *c, const char *fmt,...)
 Behaves like gl_print but stops displaying text after reaching a certain length.
int gl_printMidRaw (const glFont *ft_font, int width, double x, double y, const glColour *c, double outlineR, const char *text)
 Displays text centered in position and width.
int gl_printMid (const glFont *ft_font, const int width, double x, double y, const glColour *c, const char *fmt,...)
 Displays text centered in position and width.
int gl_printTextRaw (const glFont *ft_font, const int width, const int height, double bx, double by, int line_height, const glColour *c, double outlineR, const char *text)
 Prints a block of text that fits in the dimensions given.
int gl_printText (const glFont *ft_font, const int width, const int height, double bx, double by, int line_height, const glColour *c, const char *fmt,...)
 Prints a block of text that fits in the dimensions given.
int gl_printWidthRaw (const glFont *ft_font, const char *text)
 Gets the width that it would take to print some text.
int gl_printWidth (const glFont *ft_font, const char *fmt,...)
 Gets the width that it would take to print some text.
int gl_printHeightRaw (const glFont *ft_font, const int width, const char *text)
 Gets the height of a non-formatted string.
int gl_printHeight (const glFont *ft_font, const int width, const char *fmt,...)
 Gets the height of the text if it were printed.
int gl_printEndRaw (int *xo, int *yo, const glFont *ft_font, int width, const char *text)
 Gets the position at which text would end writing.
int gl_printEnd (int *x, int *y, const glFont *ft_font, int width, const char *fmt,...)
int gl_printLinesRaw (const glFont *ft_font, const int width, const char *text)
 Gets the number of lines of a non-formatted string.
int gl_printLines (const glFont *ft_font, const int width, const char *fmt,...)
 Gets the number of lines of the text if it were printed.
static int font_makeChar (glFontStash *stsh, font_char_t *c, uint32_t ch)
static uint32_t hashint (uint32_t a)
 Hash function for integers.
void gl_fontSetFilter (const glFont *ft_font, GLint min, GLint mag)
 Sets the minification and magnification filters for a font.
int gl_fontInit (glFont *font, const char *fname, const unsigned int h, const char *prefix, unsigned int flags)
 Initializes a font.
int gl_fontAddFallback (glFont *font, const char *fname, const char *prefix)
 Adds a fallback font to a font.
void gl_freeFont (glFont *font)
 Frees a loaded font. Caution: its glFontStash still has a slot in avail_fonts. At the time of writing, it's enough to zero it so it cannot match a future font request.
void gl_fontExit (void)
 Frees all resources associated with the font system. This also resets font ID tracking, so there's no going back.

Variables

static mat4 font_projection_mat
static FT_Library font_library = NULL
static FT_UInt prev_glyph_index
static int prev_glyph_ft_index
static glFontStashavail_fonts
glFont gl_defFont
glFont gl_smallFont
glFont gl_defFontMono
static const glColour * font_lastCol
static int font_restoreLast = 0

Detailed Description

OpenGL font rendering routines.

We use distance fields 1 to render high quality fonts with the help of some shaders. Characters are generated on demand using a texture atlas.

Definition in file font.c.

Macro Definition Documentation

◆ DEFAULT_TEXTURE_SIZE

#define DEFAULT_TEXTURE_SIZE    1024

Default size of texture caches for glyphs.

Definition at line 40 of file font.c.

◆ FONT_DISTANCE_FIELD_SIZE

#define FONT_DISTANCE_FIELD_SIZE   55

Size to render the fonts at.

Definition at line 38 of file font.c.

◆ HASH_LUT_SIZE

#define HASH_LUT_SIZE   512

Size of glyph look up table.

Definition at line 39 of file font.c.

◆ MAX_EFFECT_RADIUS

#define MAX_EFFECT_RADIUS    4

Maximum pixel distance from glyph to outline/shadow/etc.

Definition at line 36 of file font.c.

◆ MAX_ROWS

#define MAX_ROWS   64

Max number of rows per texture cache.

Definition at line 42 of file font.c.

Function Documentation

◆ font_limitSize()

size_t font_limitSize ( glFontStash * stsh,
int * width,
const char * text,
const int max )
static

Limits the text to max.

Parameters
stshFont stash to calculate width with.
widthActual width it takes up.
textText to parse.
maxMax to look for.
Returns
Number of characters that fit.

Definition at line 477 of file font.c.

◆ font_makeChar()

int font_makeChar ( glFontStash * stsh,
font_char_t * c,
uint32_t ch )
static

Definition at line 1254 of file font.c.

◆ font_nextChar()

uint32_t font_nextChar ( const char * s,
size_t * i )
static

Reads the next utf-8 sequence out of a string, updating an index. Skips font markup directives.

Todo
For now, this enforces font.c's inability to handle tabs.

Definition at line 619 of file font.c.

◆ gl_fontAddFallback()

int gl_fontAddFallback ( glFont * font,
const char * fname,
const char * prefix )

Adds a fallback font to a font.

Parameters
fontFont to add fallback to.
fnameName of the fallback to add.
prefixPrefix to use.
Returns
0 on success.

Definition at line 1776 of file font.c.

◆ gl_fontAddGlyphTex()

int gl_fontAddGlyphTex ( glFontStash * stsh,
font_char_t * ch,
glFontGlyph * glyph )
static

Adds a font glyph to the texture stash.

Definition at line 208 of file font.c.

◆ gl_fontExit()

void gl_fontExit ( void )

Frees all resources associated with the font system. This also resets font ID tracking, so there's no going back.

Definition at line 1930 of file font.c.

◆ gl_fontGetColour()

const glColour * gl_fontGetColour ( uint32_t ch)
static

Gets the colour from a character.

< Friendly

< Hostile

< Neutral

< Inert

< Restricted

< Console

Definition at line 1394 of file font.c.

◆ gl_fontGetGlyph()

glFontGlyph * gl_fontGetGlyph ( glFontStash * stsh,
uint32_t ch )
static

Gets or caches a glyph to render.

Definition at line 1480 of file font.c.

◆ gl_fontGetStash()

glFontStash * gl_fontGetStash ( const glFont * font)
static

Gets the font stash corresponding to a font.

Definition at line 200 of file font.c.

◆ gl_fontInit()

int gl_fontInit ( glFont * font,
const char * fname,
const unsigned int h,
const char * prefix,
unsigned int flags )

Initializes a font.

Parameters
fontFont to load (NULL defaults to gl_defFont).
fnameName of the font (from inside packfile).
hHeight of the font to generate.
prefixPrefix to look for the font.
flagsFlags to use when generating the font.
Returns
0 on success.

Definition at line 1670 of file font.c.

◆ gl_fontKernGlyph()

int gl_fontKernGlyph ( glFontStash * stsh,
uint32_t ch,
glFontGlyph * glyph )
static

Return the signed advance (same units as adv_x) ahead of the current char.

Definition at line 1547 of file font.c.

◆ gl_fontKernStart()

void gl_fontKernStart ( void )
static

Call at the start of a string/line.

Definition at line 1538 of file font.c.

◆ gl_fontRenderEnd()

void gl_fontRenderEnd ( void )
static

Ends the rendering engine.

Definition at line 1626 of file font.c.

◆ gl_fontRenderGlyph()

int gl_fontRenderGlyph ( glFontStash * stsh,
uint32_t ch,
const glColour * c,
int state )
static

Renders a character.

Definition at line 1570 of file font.c.

◆ gl_fontRenderStart()

void gl_fontRenderStart ( const glFontStash * stsh,
double x,
double y,
const glColour * c,
double outlineR )
static

Starts the rendering engine.

Definition at line 1338 of file font.c.

◆ gl_fontRenderStartH()

void gl_fontRenderStartH ( const glFontStash * stsh,
const mat4 * H,
const glColour * c,
double outlineR )
static

Definition at line 1347 of file font.c.

◆ gl_fontSetFilter()

void gl_fontSetFilter ( const glFont * ft_font,
GLint min,
GLint mag )

Sets the minification and magnification filters for a font.

Parameters
ft_fontFont to set filters of.
minMinification filter (GL_LINEAR on GL_NEAREST).
magMagnification filter (GL_LINEAR on GL_NEAREST).

Definition at line 1645 of file font.c.

◆ gl_fontstashAddFallback()

int gl_fontstashAddFallback ( glFontStash * stsh,
const char * fname,
unsigned int h )
static

Adds a fallback font to a stash.

Parameters
stshStash to add fallback to.
fnameName of the fallback to add.
hHeight to use for the font.
Returns
0 on success.

Definition at line 1810 of file font.c.

◆ gl_fontstashftDestroy()

void gl_fontstashftDestroy ( glFontStashFreetype * ft)
static

Frees resources referenced by a glFontStashFreetype struct.

Definition at line 1916 of file font.c.

◆ gl_freeFont()

void gl_freeFont ( glFont * font)

Frees a loaded font. Caution: its glFontStash still has a slot in avail_fonts. At the time of writing, it's enough to zero it so it cannot match a future font request.

Parameters
fontFont to free.

Definition at line 1881 of file font.c.

◆ gl_print()

void gl_print ( const glFont * ft_font,
const double x,
const double y,
const glColour * c,
const char * fmt,
... )

Prints text on screen like printf.

Defaults ft_font to gl_defFont if NULL.

Parameters
ft_fontFont to use (NULL means gl_defFont)
xX position to put text at.
yY position to put text at.
cColour to use (uses white if NULL)
fmtString formatted like printf to print.

Definition at line 725 of file font.c.

◆ gl_printEnd()

int gl_printEnd ( int * x,
int * y,
const glFont * ft_font,
int width,
const char * fmt,
... )

Definition at line 1178 of file font.c.

◆ gl_printEndRaw()

int gl_printEndRaw ( int * xo,
int * yo,
const glFont * ft_font,
int width,
const char * text )

Gets the position at which text would end writing.

Parameters
[out]xoX value it ended writing at.
[out]yoY value it ended writing at.
ft_fontFont to use.
widthWidth to fit to.
textText to calculate length of.
Returns
0 on success;

Definition at line 1107 of file font.c.

◆ gl_printHeight()

int gl_printHeight ( const glFont * ft_font,
const int width,
const char * fmt,
... )

Gets the height of the text if it were printed.

Does not display the text on screen.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont).
widthWidth to jump to next line once reached.
fmtText to get the height of in printf format.
Returns
The height of the text.

Definition at line 1082 of file font.c.

◆ gl_printHeightRaw()

int gl_printHeightRaw ( const glFont * ft_font,
const int width,
const char * text )

Gets the height of a non-formatted string.

Does not display the text on screen.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont).
widthWidth to jump to next line once reached.
textText to get the height of.
Returns
The height of the text.

Definition at line 1050 of file font.c.

◆ gl_printLineIteratorInit()

void gl_printLineIteratorInit ( glPrintLineIterator * iter,
const glFont * ft_font,
const char * text,
int width )

Initialize an iterator object for breaking text into lines.

Parameters
iterNew glPrintLineIterator.
textText to split.
ft_fontFont to use.
widthMaximum width of a line.

Definition at line 528 of file font.c.

◆ gl_printLineIteratorNext()

int gl_printLineIteratorNext ( glPrintLineIterator * iter)

Updates iter with the next line's information.

Parameters
iterAn iterator returned by gl_printLineIteratorInit.
Returns
nonzero if there's a line.

Definition at line 550 of file font.c.

◆ gl_printLines()

int gl_printLines ( const glFont * ft_font,
const int width,
const char * fmt,
... )

Gets the number of lines of the text if it were printed.

Does not display the text on screen.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont).
widthWidth to jump to next line once reached.
fmtText to get the height of in printf format.
Returns
The number of lines of he text.

Definition at line 1232 of file font.c.

◆ gl_printLinesRaw()

int gl_printLinesRaw ( const glFont * ft_font,
const int width,
const char * text )

Gets the number of lines of a non-formatted string.

Does not display the text on screen.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont).
widthWidth to jump to next line once reached.
textText to get the height of.
Returns
The number of lines of the text.

Definition at line 1203 of file font.c.

◆ gl_printMarkerRaw()

void gl_printMarkerRaw ( const glFont * ft_font,
double x,
double y,
const glColour * c,
const char * text )

Wrapper for gl_printRaw for map overlay markers.

See gl_printRaw params (minus outlineR)

Definition at line 708 of file font.c.

◆ gl_printMax()

int gl_printMax ( const glFont * ft_font,
const int max,
double x,
double y,
const glColour * c,
const char * fmt,
... )

Behaves like gl_print but stops displaying text after reaching a certain length.

Parameters
ft_fontFont to use (NULL means use gl_defFont).
maxMaximum length to reach.
xX position to display text at.
yY position to display text at.
cColour to use (NULL defaults to white).
fmtString to display formatted like printf.
Returns
The number of characters it had to suppress.

Definition at line 792 of file font.c.

◆ gl_printMaxRaw()

int gl_printMaxRaw ( const glFont * ft_font,
const int max,
double x,
double y,
const glColour * c,
double outlineR,
const char * text )

Behaves like gl_printRaw but stops displaying text after a certain distance.

Parameters
ft_fontFont to use.
maxMaximum length to reach.
xX position to display text at.
yY position to display text at.
cColour to use (NULL defaults to white).
outlineRRadius in px of outline (-1 for default, 0 for none)
textString to display.
Returns
The number of characters it had to suppress.

Definition at line 753 of file font.c.

◆ gl_printMid()

int gl_printMid ( const glFont * ft_font,
const int width,
double x,
double y,
const glColour * c,
const char * fmt,
... )

Displays text centered in position and width.

Will truncate if text is too long.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont)
widthWidth of area to center in.
xX position to display text at.
yY position to display text at.
cColour to use for text (NULL defaults to white).
fmtText to display formatted like printf.
Returns
The number of characters it had to truncate.

Definition at line 863 of file font.c.

◆ gl_printMidRaw()

int gl_printMidRaw ( const glFont * ft_font,
int width,
double x,
double y,
const glColour * c,
double outlineR,
const char * text )

Displays text centered in position and width.

Will truncate if text is too long.

Parameters
ft_fontFont to use.
widthWidth of area to center in.
xX position to display text at.
yY position to display text at.
cColour to use for text (NULL defaults to white).
outlineRRadius in px of outline (-1 for default, 0 for none)
textString to display.
Returns
The number of characters it had to truncate.

Definition at line 821 of file font.c.

◆ gl_printRaw()

void gl_printRaw ( const glFont * ft_font,
double x,
double y,
const glColour * c,
double outlineR,
const char * text )

Prints text on screen.

Defaults ft_font to gl_defFont if NULL.

Parameters
ft_fontFont to use
xX position to put text at.
yY position to put text at.
cColour to use (uses white if NULL)
outlineRRadius in px of outline (-1 for default, 0 for none)
textString to display.

Definition at line 646 of file font.c.

◆ gl_printRawH()

void gl_printRawH ( const glFont * ft_font,
const mat4 * H,
const glColour * c,
const double outlineR,
const char * text )

Prints text on screen using a transformation matrix.

Defaults ft_font to gl_defFont if NULL.

Parameters
ft_fontFont to use
HTransformation matrix to use.
cColour to use (uses white if NULL)
outlineRRadius in px of outline (-1 for default, 0 for none)
textString to display.

Definition at line 680 of file font.c.

◆ gl_printRestore()

void gl_printRestore ( const glFontRestore * restore)

Restores last colour from a restore structure.

Parameters
restoreStructure to restore.

Definition at line 425 of file font.c.

◆ gl_printRestoreClear()

void gl_printRestoreClear ( void )

Clears the restoration.

Definition at line 398 of file font.c.

◆ gl_printRestoreInit()

void gl_printRestoreInit ( glFontRestore * restore)

Initializes a restore structure.

Parameters
restoreStructure to initialize.

Definition at line 416 of file font.c.

◆ gl_printRestoreLast()

void gl_printRestoreLast ( void )

Restores last colour.

Definition at line 406 of file font.c.

◆ gl_printStore()

void gl_printStore ( glFontRestore * restore,
const char * text )

Stores the colour information from a piece of text.

Parameters
restoreStructure to save colour information to.
textText to extract colour information from.

Definition at line 463 of file font.c.

◆ gl_printStoreMax()

void gl_printStoreMax ( glFontRestore * restore,
const char * text,
int max )

Stores the colour information from a piece of text limited to max characters.

Parameters
restoreStructure to save colour information to.
textText to extract colour information from.
maxMaximum number of characters to process.

Definition at line 440 of file font.c.

◆ gl_printText()

int gl_printText ( const glFont * ft_font,
const int width,
const int height,
double bx,
double by,
int line_height,
const glColour * c,
const char * fmt,
... )

Prints a block of text that fits in the dimensions given.

Positions are based on origin being top-left.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont).
widthMaximum width to print to.
heightMaximum height to print to.
bxX position to display text at.
byY position to display text at.
line_heightHeight of each line to print.
cColour to use (NULL defaults to white).
fmtText to display formatted like printf.
Returns
0 on success. prints text with line breaks included to a maximum width and height preset

Definition at line 956 of file font.c.

◆ gl_printTextRaw()

int gl_printTextRaw ( const glFont * ft_font,
const int width,
const int height,
double bx,
double by,
int line_height,
const glColour * c,
double outlineR,
const char * text )

Prints a block of text that fits in the dimensions given.

Positions are based on origin being top-left.

Parameters
ft_fontFont to use.
widthMaximum width to print to.
heightMaximum height to print to.
bxX position to display text at.
byY position to display text at.
line_heightHeight of each line to print.
cColour to use (NULL defaults to white).
outlineRRadius in px of outline (-1 for default, 0 for none)
textString to display.
Returns
0 on success. prints text with line breaks included to a maximum width and height preset

Definition at line 895 of file font.c.

◆ gl_printWidth()

int gl_printWidth ( const glFont * ft_font,
const char * fmt,
... )

Gets the width that it would take to print some text.

Does not display text on screen.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont).
fmtText to calculate the length of.
Returns
The length of the text in pixels.

Definition at line 1026 of file font.c.

◆ gl_printWidthRaw()

int gl_printWidthRaw ( const glFont * ft_font,
const char * text )

Gets the width that it would take to print some text.

Does not display text on screen.

Parameters
ft_fontFont to use (NULL defaults to gl_defFont).
textText to calculate the length of.
Returns
The length of the text in pixels.

Definition at line 984 of file font.c.

◆ hashint()

uint32_t hashint ( uint32_t a)
static

Hash function for integers.

Taken from http://burtleburtle.net/bob/hash/integer.html (Thomas Wang). Meant to only use the low bits, not the high bits.

Definition at line 1466 of file font.c.

Variable Documentation

◆ avail_fonts

glFontStash* avail_fonts
static
Initial value:
=
NULL

Available fonts stashes. These are pointed to by the font struct exposed in font.h.

Definition at line 154 of file font.c.

◆ font_lastCol

const glColour* font_lastCol
static
Initial value:
=
NULL

Stores last colour used (activated by FONT_COLOUR_CODE).

Definition at line 163 of file font.c.

◆ font_library

FT_Library font_library = NULL
static

Global FreeType library.

Definition at line 49 of file font.c.

◆ font_projection_mat

mat4 font_projection_mat
static

OpenGL rendering stuff. Since we can't actually render with multiple threads we can be lazy and use global variables. Projection matrix.

Definition at line 48 of file font.c.

◆ font_restoreLast

int font_restoreLast = 0
static

Restore last colour.

Definition at line 165 of file font.c.

◆ gl_defFont

glFont gl_defFont

Default font.

Definition at line 158 of file font.c.

◆ gl_defFontMono

glFont gl_defFontMono

Default mono font.

Definition at line 160 of file font.c.

◆ gl_smallFont

glFont gl_smallFont

Small font.

Definition at line 159 of file font.c.

◆ prev_glyph_ft_index

int prev_glyph_ft_index
static

HACK: Index into which stsh->ft[_].face?

Definition at line 52 of file font.c.

◆ prev_glyph_index

FT_UInt prev_glyph_index
static

Index of last character drawn (for kerning).

Definition at line 51 of file font.c.