JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Font Class Reference

Font loaded from a TTF or OTF file. More...

#include <jngl/font.hpp>

Public Member Functions

 Font (const std::string &filename, unsigned int size, float strokePercentage=0)
 Creates a font from filename in size px.
 
void print (const Mat3 &modelview, const std::string &text, Rgba color) const
 Draw text using modelview in color.
 
void print (const std::string &, int x, int y)
 Uses the font to print something at x y. The color can be specified using setFontColor.
 
void print (const std::string &text, Vec2 position) const
 Draw text at position. The color can be specified using setFontColor.
 
void print (const Mat3 &modelview, const std::string &text) const
 Draw text using modelview. The color can be specified using setFontColor.
 
double getTextWidth (std::string_view) const
 Calculates the width of text in scale-independent pixels if it would be drawn with this font.
 
double getLineHeight () const
 Returns the font size in scale-independent pixels.
 
void setLineHeight (double)
 Sets the line height in scale-independent pixels used for multi-line text (i.e.
 
std::shared_ptr< FontImpl > getImpl ()
 Internal function.
 
std::shared_ptr< const FontImpl > getImpl () const
 

Detailed Description

Font loaded from a TTF or OTF file.

A common pattern is to have a global Singleton with all your fonts, e.g.:

struct Fonts : jngl::Singleton<Fonts> {
jngl::Font headline{ "Arial.ttf", 30 };
jngl::Font normal{ "Arial.ttf", 20 };
jngl::Font small{ "Arial.ttf", 10 };
jngl::Font& editor = normal;
};
Font loaded from a TTF or OTF file.
Definition font.hpp:36
Inherit from this class to create a singleton that will be destroyed when your games exits.
Definition Singleton.hpp:23

Then you can use it like this:

Fonts::handle().headline.print(jngl::modelview().translate({ 0, 0 }), "Hello");
Fonts::handle().editor.print(jngl::modelview().translate({ 0, 90 }), "World");
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.
void translate(double x, double y)
Multiplies the global ModelView matrix with a translation matrix.

Definition at line 36 of file font.hpp.

Constructor & Destructor Documentation

◆ Font()

Font ( const std::string & filename,
unsigned int size,
float strokePercentage = 0 )

Creates a font from filename in size px.

You may set strokePercentage to a positive or negative % value to increase or decrease the font size, without actually changing the position/size of the individual characters. This can be used to draw an outlined text by e.g. first drawing with a 5% stroke and then printing the same text with 0% (or even a negative stroke) over it. jngl::OutlinedFont does exactly that for you automatically.

Example: To increase the size of each character by 2px for a Font with a size of 20px, you would pass 10.f for strokePercentage.

Member Function Documentation

◆ setLineHeight()

void setLineHeight ( double )

Sets the line height in scale-independent pixels used for multi-line text (i.e.

text containing \n) passed to print(). Defaults to a value derived from the font's size.


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