libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::Font Class Reference

RAII wrapper around a TTF_Font handle. More...

#include <font.hpp>

Public Member Functions

 Font ()=default
 Default constructor — font is not loaded.
 Font (const std::string &tf)
 Load a font without specifying a size (size determined later).
 Font (const std::string &tf, int size)
 Load a font at the given point size.
 Font (TTF_Font *tf)
 Wrap an already-open TTF_Font pointer (takes ownership).
 Font (const Font &f)=delete
 Font (Font &&f)
 Move constructor — transfers ownership of the TTF handle.
 ~Font ()
 Destructor — closes the TTF_Font if loaded.
Font & operator= (const Font &f)=delete
Font & operator= (Font &&f)
 Move-assign — transfers ownership of the TTF handle.
bool tryLoadFont (const std::string &fname, int size)
 Attempt to load a font, returning false on failure.
void loadFont (const std::string &fname, int size)
 Load a font, throwing mx::Exception on failure.
std::optional< TTF_Font * > handle () const
 Return the underlying TTF_Font as an optional.
TTF_Font * unwrap () const
 Unwrap the TTF_Font pointer, throwing on null.
Wrapper< TTF_Font * > wrapper () const
 Return a Wrapper around the TTF_Font handle.

Private Attributes

TTF_Font * the_font = nullptr
 Underlying SDL_ttf font handle.

Detailed Description

RAII wrapper around a TTF_Font handle.

Loads a TrueType font from disk and manages its lifetime. The underlying TTF_Font pointer is accessible via handle(), unwrap(), or wrapper() for safe optional-style access.

Definition at line 25 of file font.hpp.

Constructor & Destructor Documentation

◆ Font() [1/6]

mx::Font::Font ( )
default

Default constructor — font is not loaded.

Referenced by Font(), Font(), operator=(), and operator=().

◆ Font() [2/6]

mx::Font::Font ( const std::string & tf)

Load a font without specifying a size (size determined later).

Parameters
tfPath to the TTF/OTF file.

Definition at line 9 of file font.cpp.

References loadFont().

◆ Font() [3/6]

mx::Font::Font ( const std::string & tf,
int size )

Load a font at the given point size.

Parameters
tfPath to the TTF/OTF file.
sizePoint size to load.

Definition at line 20 of file font.cpp.

References loadFont(), and the_font.

◆ Font() [4/6]

mx::Font::Font ( TTF_Font * tf)

Wrap an already-open TTF_Font pointer (takes ownership).

Parameters
tfExisting TTF_Font handle.

Definition at line 25 of file font.cpp.

References the_font.

◆ Font() [5/6]

mx::Font::Font ( const Font & f)
delete

References Font().

◆ Font() [6/6]

mx::Font::Font ( Font && f)

Move constructor — transfers ownership of the TTF handle.

Definition at line 27 of file font.cpp.

References Font(), and the_font.

◆ ~Font()

mx::Font::~Font ( )

Destructor — closes the TTF_Font if loaded.

Definition at line 13 of file font.cpp.

References the_font.

Member Function Documentation

◆ handle()

std::optional< TTF_Font * > mx::Font::handle ( ) const
inline

Return the underlying TTF_Font as an optional.

Returns
std::optional containing the handle if loaded, else std::nullopt.

Definition at line 77 of file font.hpp.

References the_font.

◆ loadFont()

void mx::Font::loadFont ( const std::string & fname,
int size )

Load a font, throwing mx::Exception on failure.

Parameters
fnamePath to the font file.
sizeDesired point size.

Definition at line 66 of file font.cpp.

References tryLoadFont().

Referenced by Font(), and Font().

◆ operator=() [1/2]

Font & mx::Font::operator= ( const Font & f)
delete

References Font().

◆ operator=() [2/2]

Font & mx::Font::operator= ( Font && f)

Move-assign — transfers ownership of the TTF handle.

Definition at line 33 of file font.cpp.

References Font(), and the_font.

◆ tryLoadFont()

bool mx::Font::tryLoadFont ( const std::string & fname,
int size )

Attempt to load a font, returning false on failure.

Parameters
fnamePath to the font file.
sizeDesired point size.
Returns
true if loaded successfully, false otherwise.

Definition at line 45 of file font.cpp.

References mx::system_err, and the_font.

Referenced by loadFont().

◆ unwrap()

TTF_Font * mx::Font::unwrap ( ) const
inline

Unwrap the TTF_Font pointer, throwing on null.

Returns
Raw TTF_Font pointer.
Exceptions
mx::Exceptionif the font is not loaded.

Definition at line 88 of file font.hpp.

References the_font.

◆ wrapper()

Wrapper< TTF_Font * > mx::Font::wrapper ( ) const
inline

Return a Wrapper around the TTF_Font handle.

Returns
Wrapper<TTF_Font*> that holds the font pointer or nullopt.

Definition at line 100 of file font.hpp.

References the_font.

Referenced by gl::GLText::printText_Blended(), mx::Text::printText_Blended(), gl::GLText::printText_Solid(), and mx::Text::printText_Solid().

Member Data Documentation

◆ the_font

TTF_Font* mx::Font::the_font = nullptr
private

Underlying SDL_ttf font handle.

Definition at line 107 of file font.hpp.

Referenced by Font(), Font(), Font(), handle(), operator=(), tryLoadFont(), unwrap(), wrapper(), and ~Font().


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