GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::graphics::Font2D Class Reference

Typeface describing an assortment of character glyphs that may be rendered on-demand into an expanding texture atlas, for use in Text2D rendering. More...

#include <GREM/graphics_2d/Font2D.hpp>

Classes

struct  RenderedGlyphInfo
 Information about a single glyph's entry in the texture atlas. More...
struct  GlyphMetrics
 Dimensions of a single glyph in this font, for shaping text. More...
struct  LineMetrics
 Vertical dimensions for shaping lines of text with this font. More...

Public Member Functions

 Font2D (const Filesystem &filesystem, CStringView filepath, const Font2DOptions &options={})
 Load a font from a file.
 ~Font2D ()=default
 Destructor.
 Font2D (const Font2D &)=delete
 Copying a font is not allowed, since the memory location of the file contents must remain stable.
 Font2D (Font2D &&other) noexcept=default
 Move constructor.
Font2D & operator= (const Font2D &other)=delete
 Copying a font is not allowed, since the memory location of the file contents must remain stable.
Font2D & operator= (Font2D &&other) noexcept=default
 Move assignment.
void clearRenderedGlyphs () noexcept
 Remove all rendered glyphs in the font and reset the texture atlas.
Optional< RenderedGlyphInfo > findRenderedGlyphInfo (uint32_t characterSize, char32_t codePoint) const noexcept
 Look up the information about a glyph's entry in the texture atlas for a specific code point.
Pair< RenderedGlyphInfo, bool > renderGlyph (Device &device, uint32_t characterSize, char32_t codePoint)
 Render the glyph for a specific character and store it in the texture atlas, if it has not already been rendered.
void setOptions (Device &device, const Font2DOptions &newOptions)
 Update the configuration options of this font and recreate the texture atlas if necessary to apply the new sampler options.
GlyphMetrics getGlyphMetrics (uint32_t characterSize, char32_t codePoint) const noexcept
 Get the dimensions of a single glyph in this font, for shaping text.
LineMetrics getLineMetrics (uint32_t characterSize) const noexcept
 Get the vertical dimensions for shaping lines of text with this font.
vec2 getKerning (uint32_t characterSize, char32_t left, char32_t right) const noexcept
 Get the kerning offset to use between a pair of adjacent character glyphs while shaping text.
const Texture & getAtlasTexture () const noexcept
 Get the texture atlas to use when rendering glyphs from this font.
Font2DOptions getOptions () const noexcept
 Get the current configuration options of the font.

Detailed Description

Typeface describing an assortment of character glyphs that may be rendered on-demand into an expanding texture atlas, for use in Text2D rendering.

Constructor & Destructor Documentation

◆ Font2D() [1/3]

grem::graphics::Font2D::Font2D ( const Filesystem & filesystem,
CStringView filepath,
const Font2DOptions & options = {} )

Load a font from a file.

The supported file formats are:

  • TrueType (.ttf)
  • OpenType (.otf)
Parameters
filesystemfilesystem to load the file from.
filepathinput filepath of the font file to load.
optionsfont options, see Font2DOptions.
Exceptions
File::Erroron failure to open the file.
graphics::Erroron failure to load a font from the file.
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.
Note
The only supported text encoding is Unicode.
Only TrueType fonts are fully supported. OpenType extensions that are not a part of TrueType may not work.

◆ ~Font2D()

grem::graphics::Font2D::~Font2D ( )
default

Destructor.

◆ Font2D() [2/3]

grem::graphics::Font2D::Font2D ( const Font2D & )
delete

Copying a font is not allowed, since the memory location of the file contents must remain stable.

◆ Font2D() [3/3]

grem::graphics::Font2D::Font2D ( Font2D && other)
defaultnoexcept

Move constructor.

Member Function Documentation

◆ operator=() [1/2]

Font2D & grem::graphics::Font2D::operator= ( const Font2D & other)
delete

Copying a font is not allowed, since the memory location of the file contents must remain stable.

◆ operator=() [2/2]

Font2D & grem::graphics::Font2D::operator= ( Font2D && other)
defaultnoexcept

Move assignment.

◆ clearRenderedGlyphs()

void grem::graphics::Font2D::clearRenderedGlyphs ( )
noexcept

Remove all rendered glyphs in the font and reset the texture atlas.

◆ findRenderedGlyphInfo()

Optional< RenderedGlyphInfo > grem::graphics::Font2D::findRenderedGlyphInfo ( uint32_t characterSize,
char32_t codePoint ) const
nodiscardnoexcept

Look up the information about a glyph's entry in the texture atlas for a specific code point.

Parameters
characterSizecharacter size of the glyph to search for.
codePointUnicode code point of the glyph to search for.
Returns
the rendered glyph information, see RenderedGlyphInfo, or an empty optional if the glyph has not been rendered.
See also
renderGlyph()
getAtlasTexture()

◆ renderGlyph()

Pair< RenderedGlyphInfo, bool > grem::graphics::Font2D::renderGlyph ( Device & device,
uint32_t characterSize,
char32_t codePoint )

Render the glyph for a specific character and store it in the texture atlas, if it has not already been rendered.

Parameters
devicedevice to use for rendering the glyph. Must outlive the font.
characterSizecharacter size to render the glyph at.
codePointUnicode code point of the glyph to render.
Returns
a pair of:
  • information about the rendered glyph, and
  • a bool that is true if the glyph was actually rendered, or false if the glyph had already been rendered previously.
Exceptions
graphics::Erroron failure to render the glyph.
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.
Note
If the specified glyph has already been rendered previously, no modification is made, and the already rendered glyph is returned. In this case, the function is guaranteed to not throw any exceptions.
See also
findRenderedGlyphInfo()
getGlyphMetrics()
getAtlasTexture()

◆ setOptions()

void grem::graphics::Font2D::setOptions ( Device & device,
const Font2DOptions & newOptions )

Update the configuration options of this font and recreate the texture atlas if necessary to apply the new sampler options.

Parameters
devicedevice to use for the texture atlas. Must outlive the font.
newOptionsnew font options, see Font2DOptions.
Exceptions
graphics::Erroron failure to render the glyphs.
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.
See also
getOptions()

◆ getGlyphMetrics()

GlyphMetrics grem::graphics::Font2D::getGlyphMetrics ( uint32_t characterSize,
char32_t codePoint ) const
nodiscardnoexcept

Get the dimensions of a single glyph in this font, for shaping text.

Parameters
characterSizecharacter size to get the glyph metrics of.
codePointUnicode code point to get the glyph metrics of.
Returns
the glyph metrics of the given code point at the given character size, see GlyphMetrics.
See also
findRenderedGlyphInfo()
renderGlyph()

◆ getLineMetrics()

LineMetrics grem::graphics::Font2D::getLineMetrics ( uint32_t characterSize) const
nodiscardnoexcept

Get the vertical dimensions for shaping lines of text with this font.

Parameters
characterSizecharacter size to get the line metrics of.
Returns
the line metrics at the given character size, see LineMetrics.

◆ getKerning()

vec2 grem::graphics::Font2D::getKerning ( uint32_t characterSize,
char32_t left,
char32_t right ) const
nodiscardnoexcept

Get the kerning offset to use between a pair of adjacent character glyphs while shaping text.

Parameters
characterSizecharacter size of the glyphs to get the kerning of.
leftUnicode code point of the left glyph in the adjacent pair.
rightUnicode code point of the right glyph in the adjacent pair.
Returns
if the font contains a valid glyph for both the left and the right characters, returns the additional offset to advance the position by when going from the left glyph to the right glyph. Otherwise, returns (0, 0).

◆ getAtlasTexture()

const Texture & grem::graphics::Font2D::getAtlasTexture ( ) const
inlinenodiscardnoexcept

Get the texture atlas to use when rendering glyphs from this font.

Returns
a read-only reference to a square texture containing all loaded glyphs.
See also
findRenderedGlyphInfo()
renderGlyph()

◆ getOptions()

Font2DOptions grem::graphics::Font2D::getOptions ( ) const
inlinenodiscardnoexcept

Get the current configuration options of the font.

Returns
the current font options.
See also
setOptions()

The documentation for this class was generated from the following file: