![]() |
naev 0.12.5
|
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"
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 glFontGlyph * | gl_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 glFontStash * | gl_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 glFontStash * | avail_fonts |
| glFont | gl_defFont |
| glFont | gl_smallFont |
| glFont | gl_defFontMono |
| static const glColour * | font_lastCol |
| static int | font_restoreLast = 0 |
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.
| #define DEFAULT_TEXTURE_SIZE 1024 |
| #define FONT_DISTANCE_FIELD_SIZE 55 |
| #define MAX_EFFECT_RADIUS 4 |
|
static |
|
static |
|
static |
| int gl_fontAddFallback | ( | glFont * | font, |
| const char * | fname, | ||
| const char * | prefix ) |
|
static |
| void gl_fontExit | ( | void | ) |
|
static |
|
static |
|
static |
| int gl_fontInit | ( | glFont * | font, |
| const char * | fname, | ||
| const unsigned int | h, | ||
| const char * | prefix, | ||
| unsigned int | flags ) |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
| void gl_fontSetFilter | ( | const glFont * | ft_font, |
| GLint | min, | ||
| GLint | mag ) |
|
static |
|
static |
Frees resources referenced by a glFontStashFreetype struct.
| 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.
| font | Font to free. |
| 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.
| ft_font | Font to use (NULL means gl_defFont) |
| x | X position to put text at. |
| y | Y position to put text at. |
| c | Colour to use (uses white if NULL) |
| fmt | String formatted like printf to print. |
| int gl_printEnd | ( | int * | x, |
| int * | y, | ||
| const glFont * | ft_font, | ||
| int | width, | ||
| const char * | fmt, | ||
| ... ) |
| int gl_printEndRaw | ( | int * | xo, |
| int * | yo, | ||
| const glFont * | ft_font, | ||
| int | width, | ||
| const char * | text ) |
| 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.
| ft_font | Font to use (NULL defaults to gl_defFont). |
| width | Width to jump to next line once reached. |
| fmt | Text to get the height of in printf format. |
| int gl_printHeightRaw | ( | const glFont * | ft_font, |
| const int | width, | ||
| const char * | text ) |
| void gl_printLineIteratorInit | ( | glPrintLineIterator * | iter, |
| const glFont * | ft_font, | ||
| const char * | text, | ||
| int | width ) |
Initialize an iterator object for breaking text into lines.
| iter | New glPrintLineIterator. |
| text | Text to split. |
| ft_font | Font to use. |
| width | Maximum width of a line. |
| int gl_printLineIteratorNext | ( | glPrintLineIterator * | iter | ) |
Updates iter with the next line's information.
| iter | An iterator returned by gl_printLineIteratorInit. |
| 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.
| ft_font | Font to use (NULL defaults to gl_defFont). |
| width | Width to jump to next line once reached. |
| fmt | Text to get the height of in printf format. |
| 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.
| ft_font | Font to use (NULL defaults to gl_defFont). |
| width | Width to jump to next line once reached. |
| text | Text to get the height of. |
| void gl_printMarkerRaw | ( | const glFont * | ft_font, |
| double | x, | ||
| double | y, | ||
| const glColour * | c, | ||
| const char * | text ) |
| 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.
| ft_font | Font to use (NULL means use gl_defFont). |
| max | Maximum length to reach. |
| x | X position to display text at. |
| y | Y position to display text at. |
| c | Colour to use (NULL defaults to white). |
| fmt | String to display formatted 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.
| ft_font | Font to use. |
| max | Maximum length to reach. |
| x | X position to display text at. |
| y | Y position to display text at. |
| c | Colour to use (NULL defaults to white). |
| outlineR | Radius in px of outline (-1 for default, 0 for none) |
| text | String to display. |
| 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.
| ft_font | Font to use (NULL defaults to gl_defFont) |
| width | Width of area to center in. |
| x | X position to display text at. |
| y | Y position to display text at. |
| c | Colour to use for text (NULL defaults to white). |
| fmt | Text to display formatted like printf. |
| 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.
| ft_font | Font to use. |
| width | Width of area to center in. |
| x | X position to display text at. |
| y | Y position to display text at. |
| c | Colour to use for text (NULL defaults to white). |
| outlineR | Radius in px of outline (-1 for default, 0 for none) |
| text | String to display. |
| 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.
| ft_font | Font to use |
| x | X position to put text at. |
| y | Y position to put text at. |
| c | Colour to use (uses white if NULL) |
| outlineR | Radius in px of outline (-1 for default, 0 for none) |
| text | String to display. |
| 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.
| ft_font | Font to use |
| H | Transformation matrix to use. |
| c | Colour to use (uses white if NULL) |
| outlineR | Radius in px of outline (-1 for default, 0 for none) |
| text | String to display. |
| void gl_printRestore | ( | const glFontRestore * | restore | ) |
| void gl_printRestoreInit | ( | glFontRestore * | restore | ) |
| void gl_printStore | ( | glFontRestore * | restore, |
| const char * | text ) |
| void gl_printStoreMax | ( | glFontRestore * | restore, |
| const char * | text, | ||
| int | max ) |
| 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.
| ft_font | Font to use (NULL defaults to gl_defFont). |
| width | Maximum width to print to. |
| height | Maximum height to print to. |
| bx | X position to display text at. |
| by | Y position to display text at. |
| line_height | Height of each line to print. |
| c | Colour to use (NULL defaults to white). |
| fmt | Text to display formatted like printf. |
| 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.
| ft_font | Font to use. |
| width | Maximum width to print to. |
| height | Maximum height to print to. |
| bx | X position to display text at. |
| by | Y position to display text at. |
| line_height | Height of each line to print. |
| c | Colour to use (NULL defaults to white). |
| outlineR | Radius in px of outline (-1 for default, 0 for none) |
| text | String to display. |
| int gl_printWidth | ( | const glFont * | ft_font, |
| const char * | fmt, | ||
| ... ) |
| int gl_printWidthRaw | ( | const glFont * | ft_font, |
| const char * | text ) |
|
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.
|
static |
|
static |
|
static |
|
static |
|
static |