1.8
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Font Class Reference

Manages a font and properties of a font. More...

Public Types

using Size = float
 Scalar used for font size. More...
 
enum class  Slant { normal = 0 , italic = 1 , oblique = 2 }
 Font slants. More...
 
enum class  Weight { normal = 0 , bold = 1 }
 Font weights. More...
 

Public Member Functions

 Font ()
 
 Font (const std::string &face)
 Create a font based on the supplied parameters. More...
 
 Font (const std::string &face, Font::Size size, Font::Weight weight=Weight::normal, Font::Slant slant=Slant::normal)
 Create a font based on the supplied parameters. More...
 
 Font (const unsigned char *data, size_t len, Font::Size size)
 Create a font from an in-memory font. More...
 
 Font (Font::Size size)
 Create a font based on the global default font, but with the specified size. More...
 
 Font (Font::Size size, Font::Weight weight)
 Create a font based on the global default font, but with the specified size and weight. More...
 
 Font (Font::Slant slant)
 Create a font based on the global default font, but with the specified slant. More...
 
 Font (Font::Weight weight)
 Create a font based on the global default font, but with the specified weight. More...
 
void deserialize (const std::string &name, const std::string &value, const Serializer::Attributes &attrs)
 Deserialize. More...
 
EGT_NODISCARD const std::string & face () const
 Get the face name the font. More...
 
void face (const std::string &face)
 Set the face of the font. More...
 
void on_screen_resized ()
 Recompute the font size from the screen size but only if it has not been defined by the user. More...
 
EGT_NODISCARD cairo_scaled_font_t * scaled_font () const
 Generates a FontConfig scaled font instance. More...
 
void serialize (const std::string &name, Serializer &serializer) const
 Serialize to the specified serializer. More...
 
EGT_NODISCARD Font::Size size () const
 Get the size of the font. More...
 
void size (const Font::Size &s)
 Set the size of the font. More...
 
EGT_NODISCARD Font::Slant slant () const
 Get the slant of the font. More...
 
void slant (Font::Slant s)
 Set the slant of the font. More...
 
EGT_NODISCARD Font::Weight weight () const
 Get the weight of the font. More...
 
void weight (Font::Weight w)
 Set the weight of the font. More...
 

Static Public Member Functions

static void reset_font_cache ()
 Clears any internal font cache. More...
 
static void shutdown_fonts ()
 Basically, this will clear the font cache and shutdown FontConfig which will release all memory allocated by FontConfig. More...
 

Static Public Attributes

static constexpr const char * DEFAULT_FACE = "Free Sans"
 Default font face. More...
 
static constexpr Font::Size DEFAULT_SIZE = 18.f
 Default font size. More...
 
static constexpr Font::Slant DEFAULT_SLANT = Font::Slant::normal
 Default font slant. More...
 
static constexpr Font::Weight DEFAULT_WEIGHT = Font::Weight::normal
 Default font weight. More...
 

Protected Member Functions

void direct_allocate ()
 

Protected Attributes

const unsigned char * m_data {nullptr}
 
std::string m_face {DEFAULT_FACE}
 Font face name. More...
 
size_t m_len {0}
 
shared_cairo_scaled_font_t m_scaled_font
 Only used when an in-memory font is created. More...
 
Font::Size m_size
 Font size. More...
 
Font::Slant m_slant {DEFAULT_SLANT}
 Font slant. More...
 
bool m_use_default_size {true}
 Use default size. More...
 
Font::Weight m_weight {DEFAULT_WEIGHT}
 Font weight. More...
 

Detailed Description

Manages a font and properties of a font.

A Font consists of a type face, a size, and a weight. The font face must be installed on the system in order to use it. Usually, if the specified font face cannot be found on the system, a similar font face will be selected.

Member Typedef Documentation

◆ Size

using Size = float

Scalar used for font size.

Member Enumeration Documentation

◆ Slant

enum Slant
strong

Font slants.

Enumerator
normal 
italic 
oblique 

◆ Weight

enum Weight
strong

Font weights.

Enumerator
normal 
bold 

Constructor & Destructor Documentation

◆ Font() [1/8]

Font ( )

◆ Font() [2/8]

Font ( const std::string &  face)
explicit

Create a font based on the supplied parameters.

Parameters
[in]faceThe face name of the font.

◆ Font() [3/8]

Font ( const unsigned char *  data,
size_t  len,
Font::Size  size 
)
explicit

Create a font from an in-memory font.

Parameters
[in]dataRaw memory to the font.
[in]lenSize of bytes of the ram memory.
[in]sizeThe size of the font.

◆ Font() [4/8]

Font ( const std::string &  face,
Font::Size  size,
Font::Weight  weight = Weight::normal,
Font::Slant  slant = Slant::normal 
)
explicit

Create a font based on the supplied parameters.

Parameters
[in]faceThe face name of the font.
[in]sizeThe size of the font.
[in]weightThe weight of the font.
[in]slantThe slant of the font.

◆ Font() [5/8]

Font ( Font::Size  size)
explicit

Create a font based on the global default font, but with the specified size.

Parameters
[in]sizeThe size of the font.

◆ Font() [6/8]

Font ( Font::Size  size,
Font::Weight  weight 
)
explicit

Create a font based on the global default font, but with the specified size and weight.

Parameters
[in]sizeThe size of the font.
[in]weightThe weight of the font.

◆ Font() [7/8]

Font ( Font::Weight  weight)
explicit

Create a font based on the global default font, but with the specified weight.

Parameters
[in]weightThe weight of the font.

◆ Font() [8/8]

Font ( Font::Slant  slant)
explicit

Create a font based on the global default font, but with the specified slant.

Parameters
[in]slantThe slant of the font.

Member Function Documentation

◆ deserialize()

void deserialize ( const std::string &  name,
const std::string &  value,
const Serializer::Attributes attrs 
)

Deserialize.

◆ direct_allocate()

void direct_allocate ( )
protected

◆ face() [1/2]

EGT_NODISCARD const std::string& face ( ) const
inline

Get the face name the font.

◆ face() [2/2]

void face ( const std::string &  face)
inline

Set the face of the font.

◆ on_screen_resized()

void on_screen_resized ( )

Recompute the font size from the screen size but only if it has not been defined by the user.

Note
Called from ComposerScreen::resize() for the theme and global fonts or from Widget::on_screen_resized() for local fonts.

◆ reset_font_cache()

static void reset_font_cache ( )
static

Clears any internal font cache.

This will cause any new requests for fonts to be re-generated.

◆ scaled_font()

EGT_NODISCARD cairo_scaled_font_t* scaled_font ( ) const

Generates a FontConfig scaled font instance.

Internally, this may use a font cache to limit regeneration of the same font more than once.

◆ serialize()

void serialize ( const std::string &  name,
Serializer serializer 
) const

Serialize to the specified serializer.

◆ shutdown_fonts()

static void shutdown_fonts ( )
static

Basically, this will clear the font cache and shutdown FontConfig which will release all memory allocated by FontConfig.

Typically, you should not need to call this. However, this is useful for debugging memory leaks and avoiding false positives in FontConfig. Once you call this function, requesting any Font to be created is likely to not work out well.

◆ size() [1/2]

EGT_NODISCARD Font::Size size ( ) const
inline

Get the size of the font.

◆ size() [2/2]

void size ( const Font::Size s)
inline

Set the size of the font.

◆ slant() [1/2]

EGT_NODISCARD Font::Slant slant ( ) const
inline

Get the slant of the font.

◆ slant() [2/2]

void slant ( Font::Slant  s)
inline

Set the slant of the font.

◆ weight() [1/2]

EGT_NODISCARD Font::Weight weight ( ) const
inline

Get the weight of the font.

◆ weight() [2/2]

void weight ( Font::Weight  w)
inline

Set the weight of the font.

Member Data Documentation

◆ DEFAULT_FACE

constexpr const char* DEFAULT_FACE = "Free Sans"
staticconstexpr

Default font face.

◆ DEFAULT_SIZE

constexpr Font::Size DEFAULT_SIZE = 18.f
staticconstexpr

Default font size.

◆ DEFAULT_SLANT

constexpr Font::Slant DEFAULT_SLANT = Font::Slant::normal
staticconstexpr

Default font slant.

◆ DEFAULT_WEIGHT

constexpr Font::Weight DEFAULT_WEIGHT = Font::Weight::normal
staticconstexpr

Default font weight.

◆ m_data

const unsigned char* m_data {nullptr}
protected

◆ m_face

std::string m_face {DEFAULT_FACE}
protected

Font face name.

◆ m_len

size_t m_len {0}
protected

◆ m_scaled_font

shared_cairo_scaled_font_t m_scaled_font
mutableprotected

Only used when an in-memory font is created.

◆ m_size

Font::Size m_size
protected

Font size.

◆ m_slant

Font::Slant m_slant {DEFAULT_SLANT}
protected

Font slant.

◆ m_use_default_size

bool m_use_default_size {true}
protected

Use default size.

◆ m_weight

Font::Weight m_weight {DEFAULT_WEIGHT}
protected

Font weight.