Font
Font loading from file or embedded, with dynamic size changes.
The guikit::Font class loads and manages a font for text rendering. Fonts can be loaded from a file on disk or from a built-in embedded font. A Font is used by Text to rasterize strings.
This doc assumes using namespace guikit is set and #include <guikit/font.hpp> is included.
Otherwise, use guikit::Font instead of Font.
Constructors
Font()
Creates a font from the built-in embedded font at size 16.0f.
Font();Font(uint16_t size)
Creates a font from the built-in embedded font at the given size.
Font(uint16_t size);Font(std::string fontPath, float size)
Opens the font file at fontPath at the given size.
Font(std::string fontPath, float size);Example:
Font font("/usr/share/fonts/truetype/DejaVuSans.ttf", 24);Destructor
~Font()
Closes the underlying font handle.
~Font();Available methods
setSize(float size)
Changes the font size. The font is reloaded at the new size (from the file path or the embedded font, whichever was used originally).
Font &setSize(float size);Example:
font.setSize(32);loadEmbeddedFont(float ptsize)
Loads the built-in embedded font at the given size and returns the SDL_ttf handle. Used internally by the constructors.
TTF_Font *loadEmbeddedFont(float ptsize);Full Example
#include <guikit/window.hpp>
#include <guikit/text.hpp>
using namespace guikit;
int main() {
Window window("Font Demo", 800, 600);
// Load a font from disk and give it to a Text widget.
Text label("Loaded from file");
label.setFont(Font("/usr/share/fonts/truetype/DejaVuSans.ttf", 20));
label.setMarginTop(10).setMarginLeft(10);
window.rootElement->addChild(std::make_shared<Text>(label));
// Or simply use the default embedded font at a different size.
window.rootElement->addChild<Text>("Embedded font")
.setFontSize(36)
.setMarginTop(60);
window.show();
return 0;
}When passing a Font by value to Text::setFont, the font's TTF_Font * handle is shared. Make sure the source Font object stays alive while the Text is rendered.