Inviwo 0.9.12-pre
Inviwo documentation
Loading...
Searching...
No Matches
inviwo::TextRenderer Class Reference

Render text using the FreeType font library. More...

#include <textrenderer.h>

Classes

struct  FontCache
 
struct  GlyphEntry
 

Public Member Functions

 TextRenderer (const std::filesystem::path &fontPath=font::getFont(font::FontType::Default, font::FullPath::Yes))
 
 TextRenderer (const TextRenderer &rhs)=delete
 
 TextRenderer (TextRenderer &&rhs) noexcept
 
TextRendereroperator= (const TextRenderer &rhs)=delete
 
TextRendereroperator= (TextRenderer &&rhs) noexcept
 
void setFont (const std::filesystem::path &fontPath)
 replace the currently loaded font face with a new one
 
std::tuple< utilgl::DepthMaskState, utilgl::GlBoolState, utilgl::BlendModeState, utilgl::ActivateFBOsetupRenderState (std::shared_ptr< Texture2D > texture, std::optional< vec4 > clearColor=std::nullopt)
 
void render (std::string_view str, const vec2 &posf, const vec2 &scale, const vec4 &color)
 
void render (std::string_view str, float x, float y, const vec2 &scale, const vec4 &color)
 renders the given string with the specified color at position x, y in normalized device coordinates [-1,1] using the scaling factor.
 
void render (const TextBoundingBox &textBoundingBox, const ivec2 &origin, std::string_view str, const vec4 &color)
 renders the given string with the specified color into a subregion of the current destination
 
void renderToTexture (std::shared_ptr< Texture2D > texture, std::string_view str, const vec4 &color, bool clearTexture=true)
 renders the given string with the specified color into a texture.
 
void renderToTexture (const TextTextureObject &texObject, std::string_view str, const vec4 &color, bool clearTexture=true)
 
void renderToTexture (const TextTextureObject &texObject, const size2_t &origin, const size2_t &size, std::string_view str, const vec4 &color, bool clearTexture=true)
 renders the given string with the specified color into a subregion of the texture.
 
void renderToTexture (std::shared_ptr< Texture2D > texture, const size2_t &origin, const size2_t &size, std::string_view str, const vec4 &color, bool clearTexture=true)
 
void renderToTexture (std::shared_ptr< Texture2D > texture, const std::vector< size2_t > &origin, const std::vector< size2_t > &size, const std::vector< std::string > &str, const vec4 &color, bool clearTexture=true)
 
void renderToTexture (std::shared_ptr< Texture2D > texture, const std::vector< TexAtlasEntry > &entries, bool clearTexture=true)
 
void clear (std::shared_ptr< Texture2D > texture, vec4 color)
 
vec2 computeTextSize (std::string_view str, const vec2 &scale)
 computes the glyph bounding box of a given string in normalized device coordinates using the scaling factor. The vertical height of the bounding box will be about (ascend + descend) + (number of lines - 1) times line height.
 
size2_t computeTextSize (std::string_view str)
 computes the glyph bounding box of a given string in pixels (screen space). The vertical height of the bounding box will be about (ascend + descend) + (number of lines - 1) times line height.
 
TextBoundingBox computeBoundingBox (std::string_view str)
 computes the bounding boxes of both text and all glyphs for a given string in pixels (screen space).
 
void setFontSize (int val)
 
int getFontSize () const
 
void setLineSpacing (double lineSpacing)
 sets the line spacing relative to the font size (default 0.2 = 20%)
 
double getLineSpacing () const
 
void setLineHeight (int lineHeight)
 
int getLineHeight () const
 
int getBaseLineOffset () const
 returns the offset of the baseline, which corresponds to ascent
 
int getBaseLineDescender () const
 returns the size of the font part below the baseline, which corresponds to descent
 
void configure (const FontSettings &settings)
 

Protected Types

using FontFamilyStyle
 
using GlyphMap = std::map<unsigned int, GlyphEntry>
 

Protected Member Functions

double getFontAscender () const
 
double getFontDescender () const
 
std::pair< bool, GlyphEntryrequestGlyph (FontCache &fc, unsigned int glyph)
 request glyph information from the texture atlas. The glyph will be added to the atlas if it isn't registered yet and there is space left in the atlas.
 
std::pair< bool, GlyphEntryaddGlyph (FontCache &fc, unsigned int glyph)
 
void uploadGlyph (FontCache &fc, unsigned int glyph)
 
FontCachegetFontCache ()
 
void createDefaultGlyphAtlas ()
 
std::shared_ptr< Texture2DcreateAtlasTexture (FontCache &fc)
 
FontFamilyStyle getFontTuple () const
 
std::string_view::const_iterator validateString (std::string_view str) const
 

Static Protected Member Functions

static std::shared_ptr< ShadergetShader ()
 

Protected Attributes

std::unordered_map< FontFamilyStyle, FontCacheglyphAtlas_
 
FT_Library fontlib_
 
FT_Face fontface_
 
int fontSize_
 
double lineSpacing_
 spacing between two lines in percent (default = 0.2)
 
std::shared_ptr< Shadershader_
 
FrameBufferObject fbo_
 
std::shared_ptr< Texture2DcurrTexture_
 

Static Protected Attributes

static constexpr char lf = '\n'
 
static constexpr char tab = '\t'
 
static constexpr int glyphMargin_ = 2
 

Detailed Description

Render text using the FreeType font library.

The origin is at the top-left corner. The first line starts at origin - ascender (getBaseLineOffset()). The distance between two lines is governed by either setting the line height or the line spacing. The line height defines the distance between two consecutive baselines. In contrast, the line spacing (or line gap) is given by line height minus ascend minus descend. Typically, a line spacing of 20% of the font size is used, which corresponds to a line height of 1.2 * font size.

* Origin
* +───────────────────────────ffffffffffffffff──────────────────── Top line
*                            f::::::::::::::::f    ▲          ▲
*                           f::::::::::::::::::f   │          │
*                           f::::::fffffff:::::f   │          │
*        ggggggggg   ggggg  f:::::f       ffffff   │          │
*       g:::::::::ggg::::g  f:::::f                │          │
*      g:::::::::::::::::g f:::::::ffffff          │          │
*     g::::::ggggg::::::gg f::::::::::::f          │        Ascender
*     g:::::g     g:::::g  f::::::::::::f          │          │
*     g:::::g     g:::::g  f:::::::ffffff          │          │
*     g:::::g     g:::::g   f:::::f                │          │
*     g::::::g    g:::::g   f:::::f                │          │
*     g:::::::ggggg:::::g  f:::::::f               │          │
*      g::::::::::::::::g  f:::::::f          Line height     │
*       gg::::::::::::::g  f:::::::f               │          ▼
* ────────gggggggg::::::g──fffffffff───────────────│────────────── Base line
*                 g:::::g                          │          ▲
*     gggggg      g:::::g                          │          │
*     g:::::gg   gg:::::g                          │       Descender
*      g::::::ggg:::::::g                          │          │
*       gg:::::::::::::g                           │          │
*         ggg::::::ggg                             │          ▼
* ───────────gggggg────────────────────────────────│────────────── Bottom line
*                                                  │          ▲
*                                                  │          │
*                                                  │     Line spacing
*                                                  │          │
*                                                  ▼          ▼
* ──────────────────────────────────────────────────────────────── Top line (of subsequent line)
* 

Member Typedef Documentation

◆ FontFamilyStyle

using inviwo::TextRenderer::FontFamilyStyle
protected
Initial value:
std::tuple<std::string, std::string, int>

Member Function Documentation

◆ clear()

void inviwo::TextRenderer::clear ( std::shared_ptr< Texture2D > texture,
vec4 color )

Clear the texture using the fbo of the text renderer. Useful when rendering many times into the texture.

◆ computeBoundingBox()

TextBoundingBox inviwo::TextRenderer::computeBoundingBox ( std::string_view str)

computes the bounding boxes of both text and all glyphs for a given string in pixels (screen space).

The vertical height of the textual bounding box is equal to (ascend + descend) + (number of lines - 1) times line height. The width corresponds to the maximum sum of all glyph advances per row, which are given by the respective font. Note: glyphs might extend beyond the text bounding box.

Parameters
strinput string
Returns
bounding box of the given string

◆ computeTextSize() [1/2]

size2_t inviwo::TextRenderer::computeTextSize ( std::string_view str)

computes the glyph bounding box of a given string in pixels (screen space). The vertical height of the bounding box will be about (ascend + descend) + (number of lines - 1) times line height.

Parameters
strinput string
Returns
size of the bounding box enclosing all glyphs of the input string in pixel
See also
computeBoundingBox

◆ computeTextSize() [2/2]

vec2 inviwo::TextRenderer::computeTextSize ( std::string_view str,
const vec2 & scale )

computes the glyph bounding box of a given string in normalized device coordinates using the scaling factor. The vertical height of the bounding box will be about (ascend + descend) + (number of lines - 1) times line height.

Parameters
strinput string
scalescaling factor from screen space (pixel) to normalized device coordinates ([-1,1])
Returns
size of the bounding box enclosing all glyphs of the input string in normalized device coordinates
See also
computeBoundingBox

◆ getBaseLineDescender()

int inviwo::TextRenderer::getBaseLineDescender ( ) const

returns the size of the font part below the baseline, which corresponds to descent

Returns
baseline offset

◆ getBaseLineOffset()

int inviwo::TextRenderer::getBaseLineOffset ( ) const

returns the offset of the baseline, which corresponds to ascent

Returns
baseline offset

◆ render() [1/2]

void inviwo::TextRenderer::render ( const TextBoundingBox & textBoundingBox,
const ivec2 & origin,
std::string_view str,
const vec4 & color )

renders the given string with the specified color into a subregion of the current destination

Parameters
textBoundingBoxthe text bounding box of the given str
originorigin of sub region within the destination (lower left corner, in pixel)
strinput string
colorcolor of rendered text

◆ render() [2/2]

void inviwo::TextRenderer::render ( std::string_view str,
float x,
float y,
const vec2 & scale,
const vec4 & color )

renders the given string with the specified color at position x, y in normalized device coordinates [-1,1] using the scaling factor.

Parameters
strinput string
xx component of position in normalized device coords
yy component of position in normalized device coords
scalescaling factor from screen space (pixel) to normalized device coords
colorcolor of rendered text

◆ renderToTexture() [1/2]

void inviwo::TextRenderer::renderToTexture ( const TextTextureObject & texObject,
const size2_t & origin,
const size2_t & size,
std::string_view str,
const vec4 & color,
bool clearTexture = true )

renders the given string with the specified color into a subregion of the texture.

Parameters
texObjectthe text will be rendered into this texture
originorigin of sub region within the texture (lower left corner, in pixel)
sizeextent of sub region (in pixel)
strinput string
colorcolor of rendered text
clearTextureif true, the texture is cleared before rendering the text

◆ renderToTexture() [2/2]

void inviwo::TextRenderer::renderToTexture ( std::shared_ptr< Texture2D > texture,
std::string_view str,
const vec4 & color,
bool clearTexture = true )

renders the given string with the specified color into a texture.

Parameters
texturethe text will be rendered into this texture
strinput string
colorcolor of rendered text
clearTextureif true, the texture is cleared before rendering the text

◆ requestGlyph()

std::pair< bool, GlyphEntry > inviwo::TextRenderer::requestGlyph ( FontCache & fc,
unsigned int glyph )
protected

request glyph information from the texture atlas. The glyph will be added to the atlas if it isn't registered yet and there is space left in the atlas.

Parameters
fcfont cache for the currently selected font, style, and size
glyphcharacter code of the glyph
Returns
std::pair<bool, GlyphEntry> where pair.first is true if the glyph exists and GlyphEntry refers to the respective glyph

◆ setFont()

void inviwo::TextRenderer::setFont ( const std::filesystem::path & fontPath)

replace the currently loaded font face with a new one

Parameters
fontPathfull path to the new font face
Exceptions
Exceptionif the font file could not be opened
FileExceptionif the font format is unsupported

◆ setLineSpacing()

void inviwo::TextRenderer::setLineSpacing ( double lineSpacing)

sets the line spacing relative to the font size (default 0.2 = 20%)

Parameters
lineSpacingfactor for line spacing

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