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

Facility for shaping text, according to a Font2D, into renderable glyphs. More...

#include <GREM/graphics_2d/Text2D.hpp>

Classes

struct  ShapedGlyph
 Data required to render a single shaped glyph relative to at any given starting position. More...
struct  ShapedGlyphInfo
 Additional information about a single shaped glyph, including some data that is not strictly required for simple rendering. More...
struct  ShapedLineInfo
 Information about a line of shaped glyphs, including some data that is not strictly required for simple rendering. More...
struct  ShapeResult
 Result of the text shaping functions. More...

Public Member Functions

 Text2D () noexcept=default
 Construct an empty text.
 Text2D (Font2D &font, uint32_t characterSize, UTF8StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
 Construct a shaped text from a UTF-8 string.
 Text2D (Font2D &font, uint32_t characterSize, StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
 Helper overload of Text2D() that takes an arbitrary byte string and interprets it as UTF-8.
void clear () noexcept
 Erase all shaped glyphs and reset the text to an empty state.
ShapeResult assign (Font2D &font, uint32_t characterSize, StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
 Helper function that is equivalent to clear() followed by append().
ShapeResult assign (Font2D &font, uint32_t characterSize, UTF8StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
 Helper function that is equivalent to clear() followed by append().
ShapeResult append (Font2D &font, uint32_t characterSize, UTF8StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
 Use a font to shape a string of UTF-8 encoded text into a sequence of glyphs that are ready to be drawn at a given offset, relative to any starting position.
ShapeResult append (Font2D &font, uint32_t characterSize, StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
 Helper overload of append() that takes an arbitrary byte string and interprets it as UTF-8.
Span< const ShapedGlyph > getShapedGlyphs () const noexcept
 Get the list of ShapedGlyph data for all shaped glyphs.
Span< const ShapedGlyphInfo > getShapedGlyphsInfo () const noexcept
 Get the list of ShapedGlyphInfo data for all shaped glyphs.
Span< const ShapedLineInfo > getShapedLinesInfo () const noexcept
 Get the list of ShapedLineInfo data for all shaped lines.
Box< 2, float > getBoundingBox () const noexcept
 Get the scaled bounding box of the shaped text.
vec2 getNextLineOffset () const noexcept
 Get the scaled offset where the next line would have ended up if another line had followed the last shaped line of text.
float getHorizontalAlignmentOffset (TextAlignHorizontal horizontalAlignment) const
 Get the scaled horizontal offset to apply when rendering the text with a specific horizontal alignment.
float getVerticalAlignmentOffset (TextAlignVertical verticalAlignment) const
 Get the scaled vertical offset to apply when rendering the text with a specific vertical alignment.
vec2 getAlignmentOffset (TextAlign alignment) const noexcept
 Get the scaled offset to apply when rendering the text with a specific alignment.

Detailed Description

Facility for shaping text, according to a Font2D, into renderable glyphs.

Constructor & Destructor Documentation

◆ Text2D() [1/3]

grem::graphics::Text2D::Text2D ( )
defaultnoexcept

Construct an empty text.

◆ Text2D() [2/3]

grem::graphics::Text2D::Text2D ( Font2D & font,
uint32_t characterSize,
UTF8StringView string,
vec2 offset = {0.0f, 0.0f},
vec2 scale = {1.0f, 1.0f} )
inline

Construct a shaped text from a UTF-8 string.

Parameters
fontfont to shape the glyphs with.
characterSizecharacter size to shape the glyphs at.
stringUTF-8 encoded text string to shape.
offsetrelative offset from the starting position to begin shaping at.
scalescaling to apply to the size of the shaped glyphs. The result is affected by Font2DOptions::useLinearFiltering.
Exceptions
graphics::Erroron failure to shape a 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
Right-to-left text shaping is currently not supported.
Grapheme clusters are currently not supported, and may be shaped incorrectly. Only one Unicode code point is shaped at a time.
Warning
If the string contains invalid UTF-8, the invalid code points will generate unspecified glyphs that may have any appearance.
Remarks
The best visual results are usually achieved when the text is shaped at an appropriate character size to begin with, rather than relying on the scaling of this function. As such, the scale parameter should generally be kept at (1, 1) unless many different character sizes are used with this font and there is a strict requirement on the maximum size of the texture atlas.
See also
assign()
append()

◆ Text2D() [3/3]

grem::graphics::Text2D::Text2D ( Font2D & font,
uint32_t characterSize,
StringView string,
vec2 offset = {0.0f, 0.0f},
vec2 scale = {1.0f, 1.0f} )
inline

Helper overload of Text2D() that takes an arbitrary byte string and interprets it as UTF-8.

See also
Text2D(const Font2D&, uint32_t, UTF8StringView, vec2, vec2)

Member Function Documentation

◆ clear()

void grem::graphics::Text2D::clear ( )
inlinenoexcept

Erase all shaped glyphs and reset the text to an empty state.

◆ assign() [1/2]

ShapeResult grem::graphics::Text2D::assign ( Font2D & font,
uint32_t characterSize,
StringView string,
vec2 offset = {0.0f, 0.0f},
vec2 scale = {1.0f, 1.0f} )
inline

Helper function that is equivalent to clear() followed by append().

See also
clear()
append()

◆ assign() [2/2]

ShapeResult grem::graphics::Text2D::assign ( Font2D & font,
uint32_t characterSize,
UTF8StringView string,
vec2 offset = {0.0f, 0.0f},
vec2 scale = {1.0f, 1.0f} )
inline

Helper function that is equivalent to clear() followed by append().

See also
clear()
append()

◆ append() [1/2]

ShapeResult grem::graphics::Text2D::append ( Font2D & font,
uint32_t characterSize,
UTF8StringView string,
vec2 offset = {0.0f, 0.0f},
vec2 scale = {1.0f, 1.0f} )

Use a font to shape a string of UTF-8 encoded text into a sequence of glyphs that are ready to be drawn at a given offset, relative to any starting position.

Parameters
fontfont to shape the glyphs with.
characterSizecharacter size to shape the glyphs at.
stringUTF-8 encoded text string to shape.
offsetrelative offset from the starting position to begin shaping at.
scalescaling to apply to the size of the shaped glyphs. The result is affected by Font2DOptions::useLinearFiltering.
Returns
see ShapeResult.
Exceptions
graphics::Erroron failure to shape a 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
Right-to-left text shaping is currently not supported.
Grapheme clusters are currently not supported, and may be shaped incorrectly. Only one Unicode code point is shaped at a time.
Warning
If the string contains invalid UTF-8, the invalid code points will generate unspecified glyphs that may have any appearance.
Remarks
The best visual results are usually achieved when the text is shaped at an appropriate character size to begin with, rather than relying on the scaling of this function. As such, the scale parameter should generally be kept at (1, 1) unless many different character sizes are used with this font and there is a strict requirement on the maximum size of the texture atlas.
See also
getShapedGlyphs()
getShapedGlyphsInfo()
getShapedLinesInfo()
getBoundingBox()

◆ append() [2/2]

ShapeResult grem::graphics::Text2D::append ( Font2D & font,
uint32_t characterSize,
StringView string,
vec2 offset = {0.0f, 0.0f},
vec2 scale = {1.0f, 1.0f} )
inline

Helper overload of append() that takes an arbitrary byte string and interprets it as UTF-8.

See also
append(Font2D&, uint32_t, UTF8StringView, vec2, vec2)

◆ getShapedGlyphs()

Span< const ShapedGlyph > grem::graphics::Text2D::getShapedGlyphs ( ) const
inlinenodiscardnoexcept

Get the list of ShapedGlyph data for all shaped glyphs.

Returns
a non-owning read-only random-access view over the ShapedGlyph data.
See also
assign()
append()
getShapedGlyphsInfo()
getShapedLinesInfo()

◆ getShapedGlyphsInfo()

Span< const ShapedGlyphInfo > grem::graphics::Text2D::getShapedGlyphsInfo ( ) const
inlinenodiscardnoexcept

Get the list of ShapedGlyphInfo data for all shaped glyphs.

Returns
a non-owning read-only random-access view over the ShapedGlyphInfo data.
See also
assign()
append()
getShapedGlyphs()
getShapedLinesInfo()

◆ getShapedLinesInfo()

Span< const ShapedLineInfo > grem::graphics::Text2D::getShapedLinesInfo ( ) const
inlinenodiscardnoexcept

Get the list of ShapedLineInfo data for all shaped lines.

Returns
a non-owning read-only random-access view over the ShapedLineInfo data.
See also
assign()
append()
getShapedGlyphs()
getShapedGlyphsInfo()

◆ getBoundingBox()

Box< 2, float > grem::graphics::Text2D::getBoundingBox ( ) const
inlinenodiscardnoexcept

Get the scaled bounding box of the shaped text.

Returns
the smallest axis-aligned box that spans all shaped glyph rectangles of this text, relative to the start of the baseline.

◆ getNextLineOffset()

vec2 grem::graphics::Text2D::getNextLineOffset ( ) const
inlinenodiscardnoexcept

Get the scaled offset where the next line would have ended up if another line had followed the last shaped line of text.

Returns
the next line offset.

◆ getHorizontalAlignmentOffset()

float grem::graphics::Text2D::getHorizontalAlignmentOffset ( TextAlignHorizontal horizontalAlignment) const
inlinenodiscard

Get the scaled horizontal offset to apply when rendering the text with a specific horizontal alignment.

Parameters
horizontalAlignmenthorizontal alignment mode.
Returns
the horizontal offset to apply to each rendered glyph.

◆ getVerticalAlignmentOffset()

float grem::graphics::Text2D::getVerticalAlignmentOffset ( TextAlignVertical verticalAlignment) const
inlinenodiscard

Get the scaled vertical offset to apply when rendering the text with a specific vertical alignment.

Parameters
verticalAlignmentvertical alignment mode.
Returns
the vertical offset to apply to each rendered glyph.

◆ getAlignmentOffset()

vec2 grem::graphics::Text2D::getAlignmentOffset ( TextAlign alignment) const
inlinenodiscardnoexcept

Get the scaled offset to apply when rendering the text with a specific alignment.

Parameters
alignmentalignment mode.
Returns
the offset to apply to each rendered glyph.

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