JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
font.hpp
Go to the documentation of this file.
1// Copyright 2012-2025 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include "Rgba.hpp"
8#include "Vec2.hpp"
9
10#include <memory>
11#include <string>
12
13namespace jngl {
14
15class FontImpl;
16class Mat3;
17
19class Font {
20public:
31 Font(const std::string& filename, unsigned int size, float strokePercentage = 0);
32
34 void print(const Mat3& modelview, const std::string& text, Rgba color) const;
35
37 void print(const std::string&, int x, int y);
38
40 void print(const std::string& text, Vec2 position) const;
41
43 void print(const Mat3& modelview, const std::string& text) const;
44
48
50 double getLineHeight() const;
51
54
55private:
57};
58
60void print(const std::string& text, jngl::Vec2 position);
61
63void print(const std::string& text, int xposition, int yposition);
64
66void print(const Mat3& modelview, const std::string& text);
67
70
72void setFontSize(int size);
73
79
83void setFont(const std::string& filename);
84
95void setFontByName(const std::string& name);
96
99
103void setFontColor(Rgb, float alpha = 1.f);
104
108void setFontColor(unsigned char red, unsigned char green, unsigned char blue,
109 unsigned char alpha = 255);
110
112void pushFontColor(unsigned char red, unsigned char green, unsigned char blue);
113
116
119
121void setLineHeight(double);
122
125double getTextWidth(const std::string& text);
126
127} // namespace jngl
Contains jngl::Rgba class.
Contains jngl::Vec2 class.
Font loaded from a TTF or OTF file.
Definition font.hpp:19
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.
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.
std::shared_ptr< FontImpl > getImpl()
Internal function.
void print(const std::string &text, Vec2 position) const
Draw text at position. The color can be specified using setFontColor.
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) const
Draw text using modelview. The color can be specified using setFontColor.
3x3 matrix
Definition Mat3.hpp:18
Object representing a RGB color, new version of jngl::Color (which will be deprecated in the future)
Definition Rgb.hpp:15
Object representing a RGBA color.
Definition Rgba.hpp:22
Two-dimensional vector.
Definition Vec2.hpp:36
JNGL's main namespace.
double getLineHeight()
Get line height used py print() in scale-independent pixel.
int getFontSize()
Get the font size used by print()
void pushFontColor(unsigned char red, unsigned char green, unsigned char blue)
Pushes the currently active font color on a stack and sets a new one.
void setFontSize(int size)
Change the font size used by print()
void popFontColor()
Resets the font color on the top of stack which is used by jngl::pushFontColor.
void print(const std::string &text, jngl::Vec2 position)
Print text at position.
void setFontColor(Rgba)
Sets the color and alpha value of the currently active font.
void setFontByName(const std::string &name)
Sets the currently active font by a font name.
void setLineHeight(double)
Set line height used by print() in scale-independent pixel.
void setFont(const std::string &filename)
Sets the currently active font to filename.
std::string getFont()
Returns the currently active font name.
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.
double getTextWidth(const std::string &text)
Calculates the width of text in scale-independent pixels if it would be drawn with the currently acti...