CGVIS Utils
Loading...
Searching...
No Matches
FontRenderer.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include <map>
6#include <utility>
7#include <memory>
8
9#include "bmp.h"
10#include "Vec2.h"
11#include "GLTexture2D.h"
12#include "GLArray.h"
13#include "GLBuffer.h"
14#include "Grid2D.h"
15
26
39
50
54enum class Alignment {
58};
59
68public:
70 FontEngine();
71 virtual ~FontEngine() {}
72
77
86 void render(const std::string& text, float winAspect, float height,
87 const Vec2& pos, Alignment a = Alignment::Center, const Vec4& color=Vec4{1.0f,1.0f,1.0f,1.0f});
89 void renderFixedWidth(const std::string& text, float winAspect, float width,
90 const Vec2& pos, Alignment a = Alignment::Center, const Vec4& color=Vec4{1.0f,1.0f,1.0f,1.0f});
91
93 void render(uint32_t number, float winAspect, float height,
94 const Vec2& pos, Alignment a = Alignment::Center, const Vec4& color=Vec4{1.0f,1.0f,1.0f,1.0f});
95 void renderFixedWidth(uint32_t number, float winAspect, float width,
96 const Vec2& pos, Alignment a = Alignment::Center, const Vec4& color=Vec4{1.0f,1.0f,1.0f,1.0f});
98
101
102 Vec2 getSize(const std::string& text, float winAspect, float height) const;
104 Vec2 getSizeFixedWidth(const std::string& text, float winAspect, float width) const;
106 Vec2 getSize(uint32_t number, float winAspect, float height) const;
107 Vec2 getSizeFixedWidth(uint32_t number, float winAspect, float width) const;
109
111 std::string getAllCharsString() const;
112
113 std::map<char,CharTex> chars;
114 std::map<char,CharTex> sdChars;
115
117 void setRenderAsSignedDistanceField(bool renderAsSignedDistanceField) {
118 this->renderAsSignedDistanceField = renderAsSignedDistanceField;
119 }
120
123 return renderAsSignedDistanceField;
124 }
125
126private:
127 GLProgram simpleProg;
128 GLProgram simpleDistProg;
129 GLArray simpleArray;
130 GLBuffer simpleVb;
131 bool renderAsSignedDistanceField;
132};
133
142public:
151 FontRenderer(const std::string& imageFilename,
152 const std::string& positionFilename);
153
155 FontRenderer(const Image& fontImage,
156 const std::string& positionFilename);
157
159 FontRenderer(const Image& fontImage,
160 const std::vector<CharPosition>& positions);
161
166 Image render(const std::string& text) const;
168 Image render(uint32_t number) const;
169
175 static const std::vector<CharPosition> loadPositions(const std::string& positionFilename);
176
181 std::string toCode(const std::string& varName) const;
182
187 std::shared_ptr<FontEngine> generateFontEngine() const;
188
189private:
190 Image fontImage;
191 std::vector<CharPosition> positions;
192
197 const CharPosition& findElement(char c) const;
198};
Alignment
Text alignment modes used by the OpenGL renderer.
Definition FontRenderer.h:54
@ Center
Anchor centered around the position.
Definition FontRenderer.h:57
@ Right
Anchor on the right side.
Definition FontRenderer.h:56
@ Left
Anchor on the left side.
Definition FontRenderer.h:55
RAII wrapper for an OpenGL Vertex Array Object (VAO).
Thin RAII wrapper for an OpenGL buffer object with upload/binding helpers.
RAII wrapper for an OpenGL 2D texture with byte/half/float upload helpers.
2D scalar grid (float) with sampling, arithmetic, SDF, and I/O helpers.
Mat4t< float > Mat4
Single-precision 4×4 matrix.
Definition Mat4.h:567
2D vector template with arithmetic, length utilities, and helpers.
Vec2t< float > Vec2
Float vector (x,y).
Definition Vec2.h:258
Vec2t< uint32_t > Vec2ui
32-bit unsigned integer vector (x,y).
Definition Vec2.h:262
Vec4t< float > Vec4
4D float vector (x,y,z,w).
Definition Vec4.h:337
Minimal BMP I/O utilities and a region blit helper.
void render(const std::string &text, float winAspect, float height, const Vec2 &pos, Alignment a=Alignment::Center, const Vec4 &color=Vec4{1.0f, 1.0f, 1.0f, 1.0f})
Render a UTF-8 string.
Definition FontRenderer.cpp:235
FontEngine()
Create an empty engine and initialize shaders, quad geometry, and VBOs.
Definition FontRenderer.cpp:136
std::map< char, CharTex > sdChars
Per-character SDF textures + transforms.
Definition FontRenderer.h:114
bool getRenderAsSignedDistanceField() const
Current SDF rendering flag.
Definition FontRenderer.h:122
void renderFixedWidth(const std::string &text, float winAspect, float width, const Vec2 &pos, Alignment a=Alignment::Center, const Vec4 &color=Vec4{1.0f, 1.0f, 1.0f, 1.0f})
Render with fixed total width (height adapts to keep aspect).
Definition FontRenderer.cpp:321
Vec2 getSize(const std::string &text, float winAspect, float height) const
Expected on-screen size of text for a given height.
Definition FontRenderer.cpp:284
void setRenderAsSignedDistanceField(bool renderAsSignedDistanceField)
Enable/disable signed distance field rendering.
Definition FontRenderer.h:117
std::map< char, CharTex > chars
Per-character textures + transforms.
Definition FontRenderer.h:113
virtual ~FontEngine()
Definition FontRenderer.h:71
Vec2 getSizeFixedWidth(const std::string &text, float winAspect, float width) const
Expected on-screen size of text for a fixed width.
Definition FontRenderer.cpp:311
std::string getAllCharsString() const
Return the available characters as a single concatenated string.
Definition FontRenderer.cpp:362
std::string toCode(const std::string &varName) const
Emit C++ code to recreate the atlas image and positions vector.
Definition FontRenderer.cpp:89
FontRenderer(const std::string &imageFilename, const std::string &positionFilename)
Construct from filenames.
Definition FontRenderer.cpp:42
Image render(const std::string &text) const
Compose a new image containing text using the atlas glyphs.
Definition FontRenderer.cpp:66
static const std::vector< CharPosition > loadPositions(const std::string &positionFilename)
Load character rectangles from a text file.
Definition FontRenderer.cpp:14
std::shared_ptr< FontEngine > generateFontEngine() const
Create a GPU font engine initialized from this bitmap font.
Definition FontRenderer.cpp:106
Definition GLArray.h:24
Definition GLBuffer.h:17
OpenGL shader program with helpers for creation and data binding.
Definition GLProgram.h:59
Definition GLTexture2D.h:19
Definition Image.h:22
Character box in a bitmap atlas.
Definition FontRenderer.h:34
char c
Character this rectangle corresponds to.
Definition FontRenderer.h:35
Vec2ui topLeft
Top-left corner (inclusive) of the glyph box in the atlas.
Definition FontRenderer.h:36
Vec2ui bottomRight
Bottom-right corner (exclusive) of the glyph box in the atlas.
Definition FontRenderer.h:37
Texture + transform bundle for a single glyph used by FontEngine.
Definition FontRenderer.h:43
Mat4 trans
Per-glyph translation used by the renderer.
Definition FontRenderer.h:46
Mat4 scale
Per-glyph scale to normalize to [0,1] quad space.
Definition FontRenderer.h:45
GLTexture2D tex
Glyph texture (RGBA or single-channel).
Definition FontRenderer.h:44
float width
Glyph width normalized by the maximum glyph width in the set.
Definition FontRenderer.h:47
float height
Glyph height normalized by the maximum glyph height in the set.
Definition FontRenderer.h:48