Utility class to perform text rendering with OpenGL.
TextRenderer supports the selection of different font families/faces and sizes, breaking input text into multiple lines (based on newline characters '\n'), as well as basic text alignment.
The default font family of TextRenderer::Font is automatically registered. In order to have TextRenderer support a additional font families, you have to register it using the static registerFontFamily() function. During rendering, if the requested family or face (bold/italic) is not available, it will fall back to the default family/face. This class respects the global GL::dpiScale() so that you should not apply an additional DPI scaling manually when defining font sizes.
Since the instantiation of a TextRenderer is rather expensive, this class inherits from GL::InstanceManager so that you can request a cached instance from any valid OpenGL context. The registration of font families works globally meaning that all TextRenderer instances share the same list of supported fonts.
vec2(200, 100),
"Example text\nwith multiple\nlines",
myFont);
static TextRenderer & cachedInstance()
Definition InstanceManager.h:80
void renderText(const Viewport &viewport, const vec2 &position, const std::string &text, const Font &font=Font(), AlignmentH alignH=Left, AlignmentV alignV=Baseline)
Render text at a given 2D position in pixel space.
TextRenderer()
Creates a new TextRenderer instance.
@ Shadow
Render a 1px drop shadow.
Definition TextRenderer.h:90
@ Bold
bold font face
Definition TextRenderer.h:82
Struct to describe font settings such as font family, size, or decorations.
Definition TextRenderer.h:98
Structure describing an OpenGL viewport.
Definition Types.h:158
- Note
- All text is expected to be valid UTF-8. Passing invalid UTF-8 sequences will yield undefined behavior.
|
enum | AlignmentH { Left
, Centered
, Right
} |
| Enum for horizontal alignment options. More...
|
|
enum | AlignmentV { Top
, Middle
, Baseline
, Bottom
} |
| Enum for vertical alignment options. More...
|
|
enum | Face { Regular = 0
, Bold = 1 << 0
, Italic = 1 << 1
} |
| Bitfield enumeration to describe a font face. More...
|
|
enum | Decoration { Plain = 0
, Shadow = 1 << 0
, Underline = 1 << 1
, Strikethrough = 1 << 2
} |
| Bitfield enumeration to describe the text decoration. More...
|
|
|
| TextRenderer () |
| Creates a new TextRenderer instance.
|
|
double | lineHeightPx (const Font &font=Font()) const |
| Returns an estimation of the vertical size of a single line in pixels.
|
|
vec2i | textSize (const std::string &text, const Font &font=Font()) |
| Calculate the required number of pixels to display the given text.
|
|
void | renderText (const Viewport &viewport, const vec2 &position, const std::string &text, const Font &font=Font(), AlignmentH alignH=Left, AlignmentV alignV=Baseline) |
| Render text at a given 2D position in pixel space.
|
|
void | renderTextWorld (const ViewState &state, const vec3 &position, const std::string &text, const Font &font=Font(), AlignmentH alignH=Left, AlignmentV alignV=Baseline, bool depthTest=true) |
| Render text at a given 3D position in world space.
|
|