naev 0.11.5
font.c File Reference

OpenGL font rendering routines. More...

#include "font.h"
#include "array.h"
#include "conf.h"
#include "distance_field.h"
#include "log.h"
#include "ndata.h"
#include "nfile.h"
#include "utf8.h"

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_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 = NULL
 
glFont gl_defFont
 
glFont gl_smallFont
 
glFont gl_defFontMono
 
static const glColour * font_lastCol = NULL
 
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 39 of file font.c.

◆ FONT_DISTANCE_FIELD_SIZE

#define FONT_DISTANCE_FIELD_SIZE   55

Size to render the fonts at.

Definition at line 37 of file font.c.

◆ HASH_LUT_SIZE

#define HASH_LUT_SIZE   512

Size of glyph look up table.

Definition at line 38 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 40 of file font.c.

Function Documentation

◆ font_limitSize()

static 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 456 of file font.c.

◆ font_makeChar()

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

Definition at line 1140 of file font.c.

◆ font_nextChar()

static 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 591 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 1618 of file font.c.

◆ gl_fontAddGlyphTex()

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

Adds a font glyph to the texture stash.

Definition at line 195 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 1771 of file font.c.

◆ gl_fontGetColour()

static const glColour * gl_fontGetColour ( uint32_t ch)
static

Gets the colour from a character.

< Friendly

< Hostile

< Neutral

< Inert

< Restricted

< Console

Definition at line 1271 of file font.c.

◆ gl_fontGetGlyph()

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

Gets or caches a glyph to render.

Definition at line 1327 of file font.c.

◆ gl_fontGetStash()

static glFontStash * gl_fontGetStash ( const glFont * font)
static

Gets the font stash corresponding to a font.

Definition at line 187 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 1517 of file font.c.

◆ gl_fontKernGlyph()

static 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 1394 of file font.c.

◆ gl_fontKernStart()

static void gl_fontKernStart ( void )
static

Call at the start of a string/line.

Definition at line 1386 of file font.c.

◆ gl_fontRenderEnd()

static void gl_fontRenderEnd ( void )
static

Ends the rendering engine.

Definition at line 1473 of file font.c.

◆ gl_fontRenderGlyph()

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

Renders a character.

Definition at line 1415 of file font.c.

◆ gl_fontRenderStart()

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

Starts the rendering engine.

Definition at line 1220 of file font.c.

◆ gl_fontRenderStartH()

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

Definition at line 1227 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 1492 of file font.c.

◆ gl_fontstashAddFallback()

static 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 1651 of file font.c.

◆ gl_fontstashftDestroy()

static void gl_fontstashftDestroy ( glFontStashFreetype * ft)
static

Frees resources referenced by a glFontStashFreetype struct.

Definition at line 1757 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 1723 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 691 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 1059 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 1027 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 506 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 526 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 1116 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 1086 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 673 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 757 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 721 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 835 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 788 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 617 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 648 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 405 of file font.c.

◆ gl_printRestoreClear()

void gl_printRestoreClear ( void )

Clears the restoration.

Definition at line 378 of file font.c.

◆ gl_printRestoreInit()

void gl_printRestoreInit ( glFontRestore * restore)

Initializes a restore structure.

Parameters
restoreStructure to initialize.

Definition at line 396 of file font.c.

◆ gl_printRestoreLast()

void gl_printRestoreLast ( void )

Restores last colour.

Definition at line 386 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 442 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 419 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 933 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 870 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 1001 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 961 of file font.c.

◆ hashint()

static 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 1313 of file font.c.

Variable Documentation

◆ avail_fonts

glFontStash* avail_fonts = NULL
static

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

Definition at line 150 of file font.c.

◆ font_lastCol

const glColour* font_lastCol = NULL
static

Stores last colour used (activated by FONT_COLOUR_CODE).

Definition at line 158 of file font.c.

◆ font_library

FT_Library font_library = NULL
static

Global FreeType library.

Definition at line 47 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 46 of file font.c.

◆ font_restoreLast

int font_restoreLast = 0
static

Restore last colour.

Definition at line 159 of file font.c.

◆ gl_defFont

glFont gl_defFont

Default font.

Definition at line 153 of file font.c.

◆ gl_defFontMono

glFont gl_defFontMono

Default mono font.

Definition at line 155 of file font.c.

◆ gl_smallFont

glFont gl_smallFont

Small font.

Definition at line 154 of file font.c.

◆ prev_glyph_ft_index

int prev_glyph_ft_index
static

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

Definition at line 49 of file font.c.

◆ prev_glyph_index

FT_UInt prev_glyph_index
static

Index of last character drawn (for kerning).

Definition at line 48 of file font.c.