DUDS
Distributed Update of Data from Something
duds::ui::graphics::BppFontPool Class Reference

Handles a pool of fonts and their associated string caches to make it easier to use fonts across various parts of an application. More...

#include <BppFontPool.hpp>

Inheritance diagram for duds::ui::graphics::BppFontPool:
Collaboration diagram for duds::ui::graphics::BppFontPool:

Classes

struct  FontAndCache
 Internal struct to hold a font and its string cache. More...
 

Public Member Functions

void add (const std::string &name, const BppFontSptr &font, const BppStringCacheSptr &cache)
 Adds an existing font and string cache pair to the pool. More...
 
void add (const std::string &name, BppFontSptr &&font, BppStringCacheSptr &&cache)
 Adds an existing font and string cache pair to the pool. More...
 
void addWithCache (const std::string &name, const BppFontSptr &font)
 Adds an existing font along with a newly created corresponding string cache. More...
 
void addWithCache (const std::string &name, const std::string &fontpath)
 Adds a newly loaded font along with a newly created corresponding string cache. More...
 
void addWithoutCache (const std::string &name, const BppFontSptr &font)
 Adds an existing font without a corresponding string cache. More...
 
void addWithoutCache (const std::string &name, const std::string &fontpath)
 Loads a font from an image archive file and adds it without a corresponding string cache. More...
 
void alias (const std::string &existing, const std::string &newname)
 Adds a new name for an already added font. More...
 
BppFontSptr getFont (const std::string &font) const
 Returns a shared pointer to a stored font, or an empty shared pointer if the font is not present. More...
 
BppStringCacheSptr getStringCache (const std::string &font) const
 Returns a shared pointer to a string cache, or an empty shared pointer if the font is not present. More...
 
template<class String >
BppImageSptr render (const std::string &font, const String &str, BppFont::Flags flags=BppFont::AlignLeft) const
 Renders text without going through a string cache. More...
 
template<class String >
ConstBppImageSptr text (const std::string &font, const String &str, BppFont::Flags flags=BppFont::AlignLeft) const
 Gets text from a string cache if present, or renders from the font otherwise. More...
 

Private Member Functions

void getFc (FontAndCache &fc, const std::string &font) const
 An internal function to get a FontAndCache struct for the given font. More...
 

Private Attributes

duds::general::Spinlock block
 Used for thread safety. More...
 
std::unordered_map< std::string, FontAndCachefonts
 The glyph images keyed by character. More...
 

Detailed Description

Handles a pool of fonts and their associated string caches to make it easier to use fonts across various parts of an application.

Author
Jeff Jackowski

Definition at line 23 of file BppFontPool.hpp.

Member Function Documentation

◆ add() [1/2]

void duds::ui::graphics::BppFontPool::add ( const std::string &  name,
const BppFontSptr font,
const BppStringCacheSptr cache 
)

Adds an existing font and string cache pair to the pool.

Parameters
nameA name for the font. Used as the key value to find the font and its string cache later.
fontThe font object.
cacheA string cache made to work with the font.
Exceptions
FontStringCacheMismatchErrorThe string cache does not use font as its font.

Definition at line 15 of file BppFontPool.cpp.

◆ add() [2/2]

void duds::ui::graphics::BppFontPool::add ( const std::string &  name,
BppFontSptr &&  font,
BppStringCacheSptr &&  cache 
)

Adds an existing font and string cache pair to the pool.

Parameters
nameA name for the font. Used as the key value to find the font and its string cache later.
fontThe font object. This pool will take ownership of the font.
cacheA string cache made to work with the font. This pool will take ownership of the string cache.
Exceptions
FontStringCacheMismatchErrorThe string cache does not use font as its font.

Definition at line 30 of file BppFontPool.cpp.

◆ addWithCache() [1/2]

void duds::ui::graphics::BppFontPool::addWithCache ( const std::string &  name,
const BppFontSptr font 
)

Adds an existing font along with a newly created corresponding string cache.

Parameters
nameA name for the font. Used as the key value to find the font and its string cache later.
fontThe font object.

Definition at line 66 of file BppFontPool.cpp.

◆ addWithCache() [2/2]

void duds::ui::graphics::BppFontPool::addWithCache ( const std::string &  name,
const std::string &  fontpath 
)

Adds a newly loaded font along with a newly created corresponding string cache.

Parameters
nameA name for the font. Used as the key value to find the font and its string cache later.
fontpathThe path to the image archive file with the font data.

Definition at line 80 of file BppFontPool.cpp.

◆ addWithoutCache() [1/2]

void duds::ui::graphics::BppFontPool::addWithoutCache ( const std::string &  name,
const BppFontSptr font 
)

Adds an existing font without a corresponding string cache.

Parameters
nameA name for the font. Used as the key value to find the font and its string cache later.
fontThe font object.

Definition at line 45 of file BppFontPool.cpp.

◆ addWithoutCache() [2/2]

void duds::ui::graphics::BppFontPool::addWithoutCache ( const std::string &  name,
const std::string &  fontpath 
)

Loads a font from an image archive file and adds it without a corresponding string cache.

Parameters
nameA name for the font. Used as the key value to find the font and its string cache later.
fontpathThe path to the image archive file with the font data.

Definition at line 56 of file BppFontPool.cpp.

◆ alias()

void duds::ui::graphics::BppFontPool::alias ( const std::string &  existing,
const std::string &  newname 
)

Adds a new name for an already added font.

The font and its string cache will both be available from both names, and any other aliased names.

Parameters
existingThe name of the already added font to alias.
newnameThe additional name to give the existing font.
Exceptions
FontNotFoundErrorThere is no font with the name in existing inside this pool.

Definition at line 91 of file BppFontPool.cpp.

◆ getFc()

void duds::ui::graphics::BppFontPool::getFc ( FontAndCache fc,
const std::string &  font 
) const
private

An internal function to get a FontAndCache struct for the given font.

Parameters
fcThe destination FontAndCache.
fontThe name of the font to lookup.
Exceptions
FontNotFoundErrorThe named font is not in the pool.

Definition at line 124 of file BppFontPool.cpp.

Referenced by render(), and text().

◆ getFont()

BppFontSptr duds::ui::graphics::BppFontPool::getFont ( const std::string &  font) const

Returns a shared pointer to a stored font, or an empty shared pointer if the font is not present.

Parameters
fontThe name of the font to find.
Returns
The shared pointer to the font. It will be empty if there is no font with the given name.

Definition at line 104 of file BppFontPool.cpp.

◆ getStringCache()

BppStringCacheSptr duds::ui::graphics::BppFontPool::getStringCache ( const std::string &  font) const

Returns a shared pointer to a string cache, or an empty shared pointer if the font is not present.

Parameters
fontThe name of the font with the string cache.
Returns
The shared pointer to the string cache. It will be empty if there is no font with the given name.

Definition at line 114 of file BppFontPool.cpp.

◆ render()

template<class String >
BppImageSptr duds::ui::graphics::BppFontPool::render ( const std::string &  font,
const String str,
BppFont::Flags  flags = BppFont::AlignLeft 
) const
inline

Renders text without going through a string cache.

Calls BppFont::render() and returns the result.

Template Parameters
StringThe type of the string to render. It can be std::string for UTF-8, or std::u32string for UTF-32.
Parameters
fontThe name of the font to use.
strThe string to render.
flagsThe option flags. The default is to render varying width, fixed height text with each line aligned to the left.
Returns
The image with the rendered text.

Definition at line 144 of file BppFontPool.hpp.

◆ text()

template<class String >
ConstBppImageSptr duds::ui::graphics::BppFontPool::text ( const std::string &  font,
const String str,
BppFont::Flags  flags = BppFont::AlignLeft 
) const
inline

Gets text from a string cache if present, or renders from the font otherwise.

Calls BppStringCache::text() and returns the result.

Template Parameters
StringThe type of the string to render. It can be std::string for UTF-8, or std::u32string for UTF-32.
Parameters
fontThe name of the font to use.
strThe string to render.
flagsThe option flags. The default is to render varying width, fixed height text with each line aligned to the left.
Returns
The image with the rendered text.

Definition at line 170 of file BppFontPool.hpp.

Member Data Documentation

◆ block

duds::general::Spinlock duds::ui::graphics::BppFontPool::block
mutableprivate

Used for thread safety.

Definition at line 38 of file BppFontPool.hpp.

Referenced by add(), addWithCache(), addWithoutCache(), alias(), getFc(), getFont(), and getStringCache().

◆ fonts

std::unordered_map<std::string, FontAndCache> duds::ui::graphics::BppFontPool::fonts
private

The glyph images keyed by character.

Definition at line 34 of file BppFontPool.hpp.

Referenced by add(), addWithCache(), addWithoutCache(), alias(), getFc(), getFont(), and getStringCache().


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