nomlib
Loading...
Searching...
No Matches
nom::TrueTypeFont Class Reference

TrueType fonts renderer. More...

#include <TrueTypeFont.hpp>

Inheritance diagram for nom::TrueTypeFont:
[legend]

Public Types

typedef TrueTypeFont self_type
typedef self_type * RawPtr
typedef std::shared_ptr< self_type > SharedPtr
Public Types inherited from nom::IFont
enum  FontType { NotDefined = 0 , BitmapFont , TrueTypeFont , BMFont }
typedef IFont self_type
typedef self_type * raw_ptr
typedef std::shared_ptr< self_type > shared_ptr
typedef std::unique_ptr< self_type > unique_ptr

Public Member Functions

 TrueTypeFont (void)
 Default constructor; initialize the object to sane defaults.
 ~TrueTypeFont (void)
 Destructor.
 TrueTypeFont (const TrueTypeFont &copy)
 Copy constructor.
IFont::raw_ptr clone (void) const override
 Construct a clone of the existing instance.
bool valid (void) const override
 Validity check.
enum IFont::FontType type (void) const override
TTF_Font * font (void) const
const Imageimage (uint32 character_size) const override
sint spacing (uint32 character_size) const override
 Obtain text character spacing width in pixels.
int point_size () const
int newline (uint32 character_size) const override
 Obtain font's line spacing.
int kerning (uint32 first_char, uint32 second_char, uint32 character_size) const override
 Obtain the kerning pair offsets between two glyphs.
int hinting (void) const override
 Get the font's hinting style.
const Glyphglyph (uint32 codepoint, uint32 character_size) const override
 Obtain a glyph.
int outline (void)
 Obtain font's outline size.
uint32 font_style (void) const override
 Get the rendering style of the font.
bool set_point_size (int point_size) override
 Set a new font point size.
bool set_hinting (int type) override
 Set the requested font hinting style.
bool set_outline (int outline) override
 Set font's outline size.
void set_font_style (uint32 style) override
 Set the rendering style of the font.
void set_font_kerning (bool state) override
 Set the use of kerning for the font.
bool load (const std::string &filename) override
 Load a new TrueType font from a file.
const FontMetricsmetrics (void) const override
 Obtain information about the loaded font.

Private Member Functions

bool build (uint32 character_size)
 Trigger a rebuild of the font metrics from the current font; this recalculates character sizes, coordinate origins, spacing, etc.
const GlyphPage & pages (void) const
sint sheet_width (void) const
sint sheet_height (void) const
const IntRect glyph_rect (FontPage &page, int width, int height) const
 Find a suitable rectangle within the texture for a glyph.

Private Attributes

enum IFont::FontType type_
 The type of font we are.
sint sheet_width_
 Width – in pixels – of overall texture atlas sheet.
sint sheet_height_
 Height – in pixels – of overall texture atlas sheet.
std::shared_ptr< TTF_Font > font_
 Font file data, used by SDL_ttf extension.
GlyphPage pages_
FontMetrics metrics_
 General font metric data, such as the proper value for newline spacing.
std::string filename_
sint point_size_
bool use_kerning_
 Whether or not to use font kerning.

Detailed Description

TrueType fonts renderer.

[TO BE WRITTEN]

const int MAX_POINT_SIZE = 41;
// We must first load the font into memory from a file.
// Cache the glyphs of the font's point size range that we plan on using;
// offloads the cost of re-generating the glyph cache when the end-user
// requests an increase or decrease. (An increase in load-time for a
// decrease in latency upon rescaling, which is especially noticeable on
// older platforms and mobile devices).
for( auto idx = 0; idx != MAX_FONT_POINT_SIZE; ++idx )
{
font.set_point_size( idx + 1 );
}
TrueType fonts renderer.
See also
http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf_toc.html#SEC_Contents
http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
http://chanae.walon.org/pub/ttf/ttf_glyphs.htm
http://cboard.cprogramming.com/game-programming/148396-ttf_font-gl-display-list-kerning-issue.html
SFML's sf::Font class

Definition at line 57 of file TrueTypeFont.hpp.

Member Typedef Documentation

◆ RawPtr

typedef self_type* nom::TrueTypeFont::RawPtr

Definition at line 61 of file TrueTypeFont.hpp.

◆ self_type

typedef TrueTypeFont nom::TrueTypeFont::self_type

Definition at line 60 of file TrueTypeFont.hpp.

◆ SharedPtr

typedef std::shared_ptr<self_type> nom::TrueTypeFont::SharedPtr

Definition at line 62 of file TrueTypeFont.hpp.

Constructor & Destructor Documentation

◆ TrueTypeFont()

nom::TrueTypeFont::TrueTypeFont ( void )

Default constructor; initialize the object to sane defaults.

Remarks
The SDL2_TTF extension must first be initialized; see nom::init.

◆ ~TrueTypeFont()

nom::TrueTypeFont::~TrueTypeFont ( void )

Destructor.

Remarks
The SDL2_TTF extension should be shutdown using nom::quit

Member Function Documentation

◆ build()

bool nom::TrueTypeFont::build ( uint32 character_size)
private

Trigger a rebuild of the font metrics from the current font; this recalculates character sizes, coordinate origins, spacing, etc.

Parameters
character_sizeFont's point size, in pixels, to build glyphs for.

◆ clone()

IFont::raw_ptr nom::TrueTypeFont::clone ( void ) const
overridevirtual

Construct a clone of the existing instance.

Implements nom::IFont.

◆ font_style()

uint32 nom::TrueTypeFont::font_style ( void ) const
overridevirtual

Get the rendering style of the font.

Returns
style A bit-mask composed of one or more of the following styles: TTF_STYLE_NORMAL, TTF_STYLE_BOLD, TTF_STYLE_ITALIC, TTF_STYLE_UNDERLINE, TTF_STYLE_STRIKETHROUGH.

Implements nom::IFont.

◆ glyph()

const Glyph & nom::TrueTypeFont::glyph ( uint32 codepoint,
uint32 character_size ) const
overridevirtual

Obtain a glyph.

Parameters
codepointASCII character to lookup
character_sizeReserved for future implementation
Returns
nom::Glyph structure

Implements nom::IFont.

◆ glyph_rect()

const IntRect nom::TrueTypeFont::glyph_rect ( FontPage & page,
int width,
int height ) const
private

Find a suitable rectangle within the texture for a glyph.

Parameters
pagePage of glyphs to search in
widthWidth of the rectangle
heightHeight of the rectangle
Returns
Found rectangle within the texture
Remarks
This method makes it possible to store bitmap sheets efficiently
Note
Derived from SFML's sf::Font class – thanks!

◆ hinting()

int nom::TrueTypeFont::hinting ( void ) const
overridevirtual

Get the font's hinting style.

Returns
The hinting type, one of: TTF_HINTING_NORMAL, TTF_HINTING_LIGHT, TTF_HINTING_MONO or TTF_HINTING_NONE.
Remarks
The default hinting is TTF_HINTING_NONE.

Implements nom::IFont.

◆ image()

const Image * nom::TrueTypeFont::image ( uint32 character_size) const
overridevirtual

Obtain the texture atlas used internally for rendering glyphs from

Remarks
It is assumed that the requested character size is already loaded into memory (using TrueTypeFont::set_point_size).

Implements nom::IFont.

◆ kerning()

int nom::TrueTypeFont::kerning ( uint32 first_char,
uint32 second_char,
uint32 character_size ) const
overridevirtual

Obtain the kerning pair offsets between two glyphs.

Returns
A kerning pair offset value on success, or nom::NOM_INT_MIN on failure, such as if the font in use is invalid. If font kerning is disabled, a value of zero (0) is always returned.
Note
The font's point size affects the kerning pair offsets.

Implements nom::IFont.

◆ load()

bool nom::TrueTypeFont::load ( const std::string & filename)
overridevirtual

Load a new TrueType font from a file.

Remarks
Refer to the SDL_ttf documentation for file formats supported. TTF and FON file formats are known to be supported as of this writing.

Implements nom::IFont.

◆ metrics()

const FontMetrics & nom::TrueTypeFont::metrics ( void ) const
overridevirtual

Obtain information about the loaded font.

Remarks
Implements IFont::metrics.

Implements nom::IFont.

◆ newline()

int nom::TrueTypeFont::newline ( uint32 character_size) const
overridevirtual

Obtain font's line spacing.

Parameters
character_sizePoint size in pixels
Returns
Height offset in pixels

Implements nom::IFont.

◆ outline()

int nom::TrueTypeFont::outline ( void )

Obtain font's outline size.

Parameters
character_sizeThe point size to retrieve the outline size for
Returns
Outline size in pixels

◆ set_font_kerning()

void nom::TrueTypeFont::set_font_kerning ( bool state)
overridevirtual

Set the use of kerning for the font.

Implements nom::IFont.

◆ set_font_style()

void nom::TrueTypeFont::set_font_style ( uint32 style)
overridevirtual

Set the rendering style of the font.

Parameters
styleA bit-mask composed of one or more of the following styles: TTF_STYLE_BOLD, TTF_STYLE_ITALIC, TTF_STYLE_UNDERLINE, TTF_STYLE_STRIKETHROUGH.

Implements nom::IFont.

◆ set_hinting()

bool nom::TrueTypeFont::set_hinting ( int type)
overridevirtual

Set the requested font hinting style.

Remarks
The hinting type, one of: TTF_HINTING_NORMAL, TTF_HINTING_LIGHT, TTF_HINTING_MONO or TTF_HINTING_NONE.
Note
This method call forces a rebuild of the glyph cache if the input type does not match the last known hinting.
Todo
This feature is broken.

Implements nom::IFont.

◆ set_outline()

bool nom::TrueTypeFont::set_outline ( int outline)
overridevirtual

Set font's outline size.

Parameters
outlineThe outline size in pixels
Remarks
This is an expensive method call; every glyph must be re-built!

Implements nom::IFont.

◆ set_point_size()

bool nom::TrueTypeFont::set_point_size ( int point_size)
overridevirtual

Set a new font point size.

Parameters
sizePoint size in pixels
Remarks
This is a potentially expensive call; if the glyphs for the requested point size has not been stored (generated previously), the generation of the font's ASCII glyph symbols must be done.
Note
For performance critical sections where scaling of the font occurs (think: real-time), you may want to take advantage of the glyph caching feature by making a call to this method for every used point size of the scaling range.

Implements nom::IFont.

◆ spacing()

sint nom::TrueTypeFont::spacing ( uint32 character_size) const
overridevirtual

Obtain text character spacing width in pixels.

Returns
The width applied when the space carriage is encountered when rendered.

Implements nom::IFont.

◆ type()

enum IFont::FontType nom::TrueTypeFont::type ( void ) const
overridevirtual

Implements nom::IFont.

◆ valid()

bool nom::TrueTypeFont::valid ( void ) const
overridevirtual

Validity check.

Implements nom::IFont.

Member Data Documentation

◆ filename_

std::string nom::TrueTypeFont::filename_
private

Store the file path so we can change font sizes on the fly

Note
Replace with FT_Face face (using FreeType2 library)

Definition at line 249 of file TrueTypeFont.hpp.

◆ font_

std::shared_ptr<TTF_Font> nom::TrueTypeFont::font_
private

Font file data, used by SDL_ttf extension.

Definition at line 235 of file TrueTypeFont.hpp.

◆ metrics_

FontMetrics nom::TrueTypeFont::metrics_
private

General font metric data, such as the proper value for newline spacing.

Definition at line 242 of file TrueTypeFont.hpp.

◆ pages_

GlyphPage nom::TrueTypeFont::pages_
mutableprivate

Table mapping a character size to its page – a texture atlas combined with corresponding glyphs data.

Definition at line 239 of file TrueTypeFont.hpp.

◆ point_size_

sint nom::TrueTypeFont::point_size_
private

Font point size (in pixels)

Note
This instance variable is obsolete with FreeType2; see FT_Face->size->metrics.x_ppem

Definition at line 257 of file TrueTypeFont.hpp.

◆ sheet_height_

sint nom::TrueTypeFont::sheet_height_
private

Height – in pixels – of overall texture atlas sheet.

Definition at line 232 of file TrueTypeFont.hpp.

◆ sheet_width_

sint nom::TrueTypeFont::sheet_width_
private

Width – in pixels – of overall texture atlas sheet.

Definition at line 229 of file TrueTypeFont.hpp.

◆ type_

enum IFont::FontType nom::TrueTypeFont::type_
private

The type of font we are.

Definition at line 226 of file TrueTypeFont.hpp.

◆ use_kerning_

bool nom::TrueTypeFont::use_kerning_
private

Whether or not to use font kerning.

Remarks
Default is true.

Definition at line 262 of file TrueTypeFont.hpp.


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