1.10
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...

#include <font.h>

Public Types

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

Static Public Member Functions

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

Static Public Attributes

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

Protected Member Functions

void direct_allocate ()
 

Protected Attributes

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

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 class Slant
strong

Font slants.

Enumerator
normal 
italic 
oblique 

◆ Weight

enum class 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.