GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Text2D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_2D_TEXT_2D_HPP
5#define GREM_GRAPHICS_2D_TEXT_2D_HPP
6
8
14#include <GREM/core/math.hpp>
15
16#include <new> // std::launder
17
18namespace grem::graphics {
19
20class Font2D; // Forward declaration, to avoid including Font2D.hpp.
21
35
49
82
83inline constexpr TextAlign TextAlign::NONE{
84 .horizontal = TextAlignHorizontal::NONE,
85 .vertical = TextAlignVertical::NONE,
86};
87
88inline constexpr TextAlign TextAlign::FIRST_LINE_START_TOP{
91};
92
93inline constexpr TextAlign TextAlign::FIRST_LINE_START_BASE{
96};
97
101};
102
106};
107
111};
112
116};
117
121};
122
126};
127
129 .horizontal = TextAlignHorizontal::CENTER,
131};
132
134 .horizontal = TextAlignHorizontal::CENTER,
136};
137
139 .horizontal = TextAlignHorizontal::CENTER,
141};
142
144 .horizontal = TextAlignHorizontal::CENTER,
146};
147
148inline constexpr TextAlign TextAlign::CENTER{
149 .horizontal = TextAlignHorizontal::CENTER,
150 .vertical = TextAlignVertical::CENTER,
151};
152
154 .horizontal = TextAlignHorizontal::CENTER,
155 .vertical = TextAlignVertical::NONE,
156};
157
159 .horizontal = TextAlignHorizontal::NONE,
160 .vertical = TextAlignVertical::CENTER,
161};
162
164 .horizontal = TextAlignHorizontal::CENTER,
165 .vertical = TextAlignVertical::BOTTOM,
166};
167
169 .horizontal = TextAlignHorizontal::CENTER,
170 .vertical = TextAlignVertical::TOP,
171};
172
174 .horizontal = TextAlignHorizontal::LEFT,
175 .vertical = TextAlignVertical::CENTER,
176};
177
179 .horizontal = TextAlignHorizontal::RIGHT,
180 .vertical = TextAlignVertical::CENTER,
181};
182
183inline constexpr TextAlign TextAlign::LEFT{
184 .horizontal = TextAlignHorizontal::LEFT,
185 .vertical = TextAlignVertical::NONE,
186};
187
188inline constexpr TextAlign TextAlign::RIGHT{
189 .horizontal = TextAlignHorizontal::RIGHT,
190 .vertical = TextAlignVertical::NONE,
191};
192
193inline constexpr TextAlign TextAlign::BOTTOM{
194 .horizontal = TextAlignHorizontal::NONE,
195 .vertical = TextAlignVertical::BOTTOM,
196};
197
198inline constexpr TextAlign TextAlign::TOP{
199 .horizontal = TextAlignHorizontal::NONE,
200 .vertical = TextAlignVertical::TOP,
201};
202
206class Text2D {
207public:
222
236
252
256 struct ShapeResult {
266
272 };
273
277 Text2D() noexcept = default;
278
312 Text2D(Font2D& font, uint32_t characterSize, UTF8StringView string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
313 append(font, characterSize, string, offset, scale);
314 }
315
322 Text2D(Font2D& font, uint32_t characterSize, StringView string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
323 append(font, characterSize, string, offset, scale);
324 }
325
329 void clear() noexcept {
330 shapedGlyphs.clear();
331 shapedGlyphsInfo.clear();
332 shapedLinesInfo.clear();
333 boundingBox = {};
334 }
335
342 ShapeResult assign(Font2D& font, uint32_t characterSize, StringView string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
343 clear();
344 return append(font, characterSize, string, offset, scale);
345 }
346
353 ShapeResult assign(Font2D& font, uint32_t characterSize, UTF8StringView string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
354 clear();
355 return append(font, characterSize, string, offset, scale);
356 }
357
397 GREM_API(graphics_2d) ShapeResult append(Font2D& font, uint32_t characterSize, UTF8StringView string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f});
398
405 ShapeResult append(Font2D& font, uint32_t characterSize, StringView string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
406 static_assert(sizeof(char) == sizeof(char8_t));
407 static_assert(alignof(char) == alignof(char8_t));
408 return append(font, characterSize, UTF8StringView{std::launder(reinterpret_cast<const char8_t*>(string.data())), string.size()}, offset, scale);
409 }
410
422 [[nodiscard]] Span<const ShapedGlyph> getShapedGlyphs() const noexcept {
423 return shapedGlyphs;
424 }
425
437 [[nodiscard]] Span<const ShapedGlyphInfo> getShapedGlyphsInfo() const noexcept {
438 return shapedGlyphsInfo;
439 }
440
452 [[nodiscard]] Span<const ShapedLineInfo> getShapedLinesInfo() const noexcept {
453 return shapedLinesInfo;
454 }
455
462 [[nodiscard]] Box<2, float> getBoundingBox() const noexcept {
463 return boundingBox;
464 }
465
472 [[nodiscard]] vec2 getNextLineOffset() const noexcept {
473 if (!shapedLinesInfo.empty()) {
474 const ShapedLineInfo& lastLine = shapedLinesInfo.back();
475 return {
476 lastLine.shapedOffset.x,
477 lastLine.shapedOffset.y - lastLine.shapedSize.y,
478 };
479 }
480 return {};
481 }
482
491 [[nodiscard]] float getHorizontalAlignmentOffset(TextAlignHorizontal horizontalAlignment) const {
492 switch (horizontalAlignment) {
493 case TextAlignHorizontal::NONE: break;
495 if (!shapedLinesInfo.empty()) {
496 const ShapedLineInfo& firstLine = shapedLinesInfo.front();
497 return round(-firstLine.shapedOffset.x);
498 }
499 break;
501 if (!shapedLinesInfo.empty()) {
502 const ShapedLineInfo& firstLine = shapedLinesInfo.front();
503 return round(-(firstLine.shapedOffset.x + firstLine.shapedSize.x));
504 }
505 break;
507 if (!shapedLinesInfo.empty()) {
508 const ShapedLineInfo& lastLine = shapedLinesInfo.back();
509 return round(-lastLine.shapedOffset.x);
510 }
511 break;
513 if (!shapedLinesInfo.empty()) {
514 const ShapedLineInfo& lastLine = shapedLinesInfo.back();
515 return round(-(lastLine.shapedOffset.x + lastLine.shapedSize.x));
516 }
517 break;
518 case TextAlignHorizontal::LEFT: return round(-boundingBox.min.x);
519 case TextAlignHorizontal::CENTER: return round(-0.5f * (boundingBox.max.x - boundingBox.min.x) - boundingBox.min.x);
520 case TextAlignHorizontal::RIGHT: return round(-boundingBox.max.x);
521 }
522 return 0.0f;
523 }
524
533 [[nodiscard]] float getVerticalAlignmentOffset(TextAlignVertical verticalAlignment) const {
534 switch (verticalAlignment) {
535 case TextAlignVertical::NONE: break;
537 if (!shapedLinesInfo.empty()) {
538 const ShapedLineInfo& firstLine = shapedLinesInfo.front();
539 return round(-(firstLine.shapedOffset.y + firstLine.shapedAscender));
540 }
541 break;
543 if (!shapedLinesInfo.empty()) {
544 const ShapedLineInfo& firstLine = shapedLinesInfo.front();
545 return round(-firstLine.shapedOffset.y);
546 }
547 break;
549 if (!shapedLinesInfo.empty()) {
550 const ShapedLineInfo& lastLine = shapedLinesInfo.back();
551 return round(-(lastLine.shapedOffset.y + lastLine.shapedAscender));
552 }
553 break;
555 if (!shapedLinesInfo.empty()) {
556 const ShapedLineInfo& lastLine = shapedLinesInfo.back();
557 return round(-lastLine.shapedOffset.y);
558 }
559 break;
560 case TextAlignVertical::BOTTOM: return round(-boundingBox.min.y);
561 case TextAlignVertical::CENTER: return round(-0.5f * (boundingBox.max.y - boundingBox.min.y) - boundingBox.min.y);
562 case TextAlignVertical::TOP: return round(-boundingBox.max.y);
563 }
564 return 0.0f;
565 }
566
575 [[nodiscard]] vec2 getAlignmentOffset(TextAlign alignment) const noexcept {
576 return {getHorizontalAlignmentOffset(alignment.horizontal), getVerticalAlignmentOffset(alignment.vertical)};
577 }
578
579private:
580 Buffer<ShapedGlyph> shapedGlyphs{};
581 Buffer<ShapedGlyphInfo> shapedGlyphsInfo{};
582 Buffer<ShapedLineInfo> shapedLinesInfo{};
583 Box<2, float> boundingBox{};
584};
585
586} // namespace grem::graphics
587
588#endif
Definition Buffer.hpp:28
Definition Span.hpp:146
Typeface describing an assortment of character glyphs that may be rendered on-demand into an expandin...
Definition Font2D.hpp:66
vec2 getNextLineOffset() const noexcept
Get the scaled offset where the next line would have ended up if another line had followed the last s...
Definition Text2D.hpp:472
float getVerticalAlignmentOffset(TextAlignVertical verticalAlignment) const
Get the scaled vertical offset to apply when rendering the text with a specific vertical alignment.
Definition Text2D.hpp:533
Box< 2, float > getBoundingBox() const noexcept
Get the scaled bounding box of the shaped text.
Definition Text2D.hpp:462
Span< const ShapedGlyph > getShapedGlyphs() const noexcept
Get the list of ShapedGlyph data for all shaped glyphs.
Definition Text2D.hpp:422
vec2 getAlignmentOffset(TextAlign alignment) const noexcept
Get the scaled offset to apply when rendering the text with a specific alignment.
Definition Text2D.hpp:575
float getHorizontalAlignmentOffset(TextAlignHorizontal horizontalAlignment) const
Get the scaled horizontal offset to apply when rendering the text with a specific horizontal alignmen...
Definition Text2D.hpp:491
ShapeResult append(Font2D &font, uint32_t characterSize, UTF8StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Use a font to shape a string of UTF-8 encoded text into a sequence of glyphs that are ready to be dra...
ShapeResult assign(Font2D &font, uint32_t characterSize, UTF8StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper function that is equivalent to clear() followed by append().
Definition Text2D.hpp:353
ShapeResult assign(Font2D &font, uint32_t characterSize, StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper function that is equivalent to clear() followed by append().
Definition Text2D.hpp:342
ShapeResult append(Font2D &font, uint32_t characterSize, StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper overload of append() that takes an arbitrary byte string and interprets it as UTF-8.
Definition Text2D.hpp:405
Span< const ShapedLineInfo > getShapedLinesInfo() const noexcept
Get the list of ShapedLineInfo data for all shaped lines.
Definition Text2D.hpp:452
void clear() noexcept
Erase all shaped glyphs and reset the text to an empty state.
Definition Text2D.hpp:329
Span< const ShapedGlyphInfo > getShapedGlyphsInfo() const noexcept
Get the list of ShapedGlyphInfo data for all shaped glyphs.
Definition Text2D.hpp:437
Text2D() noexcept=default
Construct an empty text.
Text2D(Font2D &font, uint32_t characterSize, StringView string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper overload of Text2D() that takes an arbitrary byte string and interprets it as UTF-8.
Definition Text2D.hpp:322
Definition buffer_layouts.hpp:20
@ NONE
Ignore the depth buffer.
Definition shaders.hpp:59
TextAlignHorizontal
Horizontal text alignment mode.
Definition Text2D.hpp:25
@ RIGHT
Align to the right of the bounding box of the text.
Definition Text2D.hpp:33
@ LEFT
Align to the left of the bounding box of the text.
Definition Text2D.hpp:31
@ LAST_LINE_START
Align to the start of the first glyph in the last line of the text.
Definition Text2D.hpp:29
@ LAST_LINE_END
Align to the end of the last glyph in the last line of the text.
Definition Text2D.hpp:30
@ FIRST_LINE_END
Align to the end of the last glyph in the first line of the text.
Definition Text2D.hpp:28
@ NONE
No horizontal alignment.
Definition Text2D.hpp:26
@ CENTER
Align to the horizontal center of the bounding box of the text.
Definition Text2D.hpp:32
@ FIRST_LINE_START
Align to the start of the first glyph in the first line of the text.
Definition Text2D.hpp:27
TextAlignVertical
Vertical text alignment mode.
Definition Text2D.hpp:39
@ LAST_LINE_TOP
Align to the ascender of the last line in the text.
Definition Text2D.hpp:43
@ BOTTOM
Align to the bottom of the bounding box of the text.
Definition Text2D.hpp:45
@ LAST_LINE_BASE
Align to the baseline of the last line in the text.
Definition Text2D.hpp:44
@ FIRST_LINE_TOP
Align to the ascender of the first line in the text.
Definition Text2D.hpp:41
@ TOP
Align to the top of the bounding box of the text.
Definition Text2D.hpp:47
@ FIRST_LINE_BASE
Align to the baseline of the first line in the text.
Definition Text2D.hpp:42
@ NONE
No vertical alignment.
Definition Text2D.hpp:40
@ CENTER
Align to the vertical center of the bounding box of the text.
Definition Text2D.hpp:46
vec< 2, float > vec2
Definition math.hpp:438
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL round(vec< N, T > a)
Definition math.hpp:1418
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL scale(vec< 2, T > scale)
Create a scale matrix describing a 2D scale.
Definition math.hpp:2665
StringViewBase< char8_t, std::char_traits< char8_t > > UTF8StringView
Definition StringView.hpp:17
Generic axis-aligned box shape with minimum and maximum extents in N-dimensional space.
Definition geometry.hpp:149
Result of the text shaping functions.
Definition Text2D.hpp:256
size_t shapedGlyphOffset
Index, into the lists returned by getShapedGlyphs() and getShapedGlyphsInfo(), of the ShapedGlyph and...
Definition Text2D.hpp:265
size_t shapedLineOffset
Index, into the list returned by getShapedLinesInfo(), of the ShapedLineInfo corresponding to the fir...
Definition Text2D.hpp:271
Additional information about a single shaped glyph, including some data that is not strictly required...
Definition Text2D.hpp:230
size_t shapedLineIndex
Index of the ShapedLineInfo corresponding to the line that this glyph is part of.
Definition Text2D.hpp:233
size_t stringOffset
Byte offset in the input string of the first code unit that this glyph originated from.
Definition Text2D.hpp:234
vec2 shapedAdvance
Scaled offset to apply in order to advance to the next glyph position, including kerning.
Definition Text2D.hpp:232
vec2 shapedOffset
Scaled offset from the starting position to draw this glyph at, in pixels.
Definition Text2D.hpp:231
Data required to render a single shaped glyph relative to at any given starting position.
Definition Text2D.hpp:215
uint32_t characterSize
Character size that this glyph was shaped at.
Definition Text2D.hpp:219
char32_t codePoint
Unicode code point of this glyph.
Definition Text2D.hpp:220
Font2D * font
Non-owning read-only non-null pointer to the font used to shape this glyph.
Definition Text2D.hpp:216
vec2 shapedSize
Scaled size of this glyph's rectangle, in pixels.
Definition Text2D.hpp:218
vec2 shapedOffset
Scaled offset from the starting position to draw this glyph at, in pixels.
Definition Text2D.hpp:217
Information about a line of shaped glyphs, including some data that is not strictly required for simp...
Definition Text2D.hpp:244
vec2 shapedOffset
Scaled offset of the baseline at the start of this line of text.
Definition Text2D.hpp:245
size_t shapedGlyphOffset
Index of the ShapedGlyph and ShapedGlyphInfo corresponding to the first glyph that is part of this li...
Definition Text2D.hpp:249
float shapedAscender
Scaled total ascender of this line.
Definition Text2D.hpp:247
size_t stringOffset
Byte offset in the input string of the first code unit that the first glyph that is part of this line...
Definition Text2D.hpp:250
float shapedDescender
Scaled total descender of this line.
Definition Text2D.hpp:248
vec2 shapedSize
Scaled total size of this line.
Definition Text2D.hpp:246
Text alignment mode.
Definition Text2D.hpp:53
static const TextAlign CENTER_HORIZONTALLY_LAST_LINE_TOP
Align to the horizontal center of the bounding box on the last line's ascender.
Definition Text2D.hpp:65
static const TextAlign CENTER_HORIZONTALLY_BOTTOM
Align to the bottom center of the bounding box of the text.
Definition Text2D.hpp:70
static const TextAlign LEFT
Align to the left of the bounding box of the text.
Definition Text2D.hpp:74
TextAlignHorizontal horizontal
Horizontal alignment mode.
Definition Text2D.hpp:79
static const TextAlign NONE
No alignment.
Definition Text2D.hpp:54
static const TextAlign CENTER_VERTICALLY
Align to the vertical center of the bounding box of the text.
Definition Text2D.hpp:69
static const TextAlign RIGHT
Align to the right of the bounding box of the text.
Definition Text2D.hpp:75
static const TextAlign CENTER
Align to the center of the bounding box of the text.
Definition Text2D.hpp:67
static const TextAlign LAST_LINE_END_BASE
Align to the end of the last line's baseline.
Definition Text2D.hpp:62
static const TextAlign LAST_LINE_START_TOP
Align to the start of the last line's ascender.
Definition Text2D.hpp:59
static const TextAlign FIRST_LINE_START_TOP
Align to the start of the first line's ascender.
Definition Text2D.hpp:55
static const TextAlign TOP
Align to the top of the bounding box of the text.
Definition Text2D.hpp:77
static const TextAlign LAST_LINE_END_TOP
Align to the end of the last line's ascender.
Definition Text2D.hpp:61
static const TextAlign CENTER_VERTICALLY_RIGHT
Align to the right center of the bounding box of the text.
Definition Text2D.hpp:73
static const TextAlign FIRST_LINE_END_BASE
Align to the end of the first line's baseline.
Definition Text2D.hpp:58
static const TextAlign CENTER_HORIZONTALLY_LAST_LINE_BASE
Align to the horizontal center of the bounding box on the last line's baseline.
Definition Text2D.hpp:66
static const TextAlign CENTER_HORIZONTALLY
Align to the horizontal center of the bounding box of the text.
Definition Text2D.hpp:68
static const TextAlign LAST_LINE_START_BASE
Align to the start of the last line's baseline.
Definition Text2D.hpp:60
static const TextAlign CENTER_HORIZONTALLY_FIRST_LINE_BASE
Align to the horizontal center of the bounding box on the first line's baseline.
Definition Text2D.hpp:64
static const TextAlign BOTTOM
Align to the bottom of the bounding box of the text.
Definition Text2D.hpp:76
static const TextAlign CENTER_VERTICALLY_LEFT
Align to the left center of the bounding box of the text.
Definition Text2D.hpp:72
static const TextAlign CENTER_HORIZONTALLY_FIRST_LINE_TOP
Align to the horizontal center of the bounding box on the first line's ascender.
Definition Text2D.hpp:63
TextAlignVertical vertical
Vertical alignment mode.
Definition Text2D.hpp:80
static const TextAlign FIRST_LINE_START_BASE
Align to the start of the first line's baseline.
Definition Text2D.hpp:56
static const TextAlign FIRST_LINE_END_TOP
Align to the end of the first line's ascender.
Definition Text2D.hpp:57
static const TextAlign CENTER_HORIZONTALLY_TOP
Align to the top center of the bounding box of the text.
Definition Text2D.hpp:71