libdonut 2.3.6
Application framework for cross-platform game development in C++20
Loading...
Searching...
No Matches
Text.hpp
Go to the documentation of this file.
1#ifndef DONUT_GRAPHICS_TEXT_HPP
2#define DONUT_GRAPHICS_TEXT_HPP
3
4#include <donut/math.hpp>
5
6#include <cstddef> // std::size_t
7#include <limits> // std::numeric_limits
8#include <span> // std::span
9#include <string_view> // std::string_view, std::u8string_view
10#include <vector> // std::vector
11
12namespace donut::graphics {
13
14class Font; // Forward declaration, to avoid including Font.hpp.
15
19class Text {
20public:
35
46 std::size_t shapedLineIndex;
47 std::size_t stringOffset;
48 };
49
60 std::size_t shapedGlyphOffset;
61 std::size_t stringOffset;
62 };
63
67 struct ShapeResult {
76 std::size_t shapedGlyphOffset;
77
82 std::size_t shapedLineOffset;
83 };
84
88 Text() noexcept = default;
89
120 Text(Font& font, u32 characterSize, std::u8string_view string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
121 shape(font, characterSize, string, offset, scale);
122 }
123
130 Text(Font& font, u32 characterSize, std::string_view string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
131 shape(font, characterSize, string, offset, scale);
132 }
133
137 void clear() noexcept {
138 shapedGlyphs.clear();
139 shapedGlyphsInfo.clear();
140 shapedLinesInfo.clear();
141 minExtent = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
142 maxExtent = {-std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity()};
143 }
144
183 ShapeResult shape(Font& font, u32 characterSize, std::u8string_view string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f});
184
191 ShapeResult shape(Font& font, u32 characterSize, std::string_view string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
192 static_assert(sizeof(char) == sizeof(char8_t));
193 static_assert(alignof(char) == alignof(char8_t));
194 return shape(font, characterSize, std::u8string_view{reinterpret_cast<const char8_t*>(string.data()), string.size()}, offset, scale);
195 }
196
203 ShapeResult reshape(Font& font, u32 characterSize, std::string_view string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
204 clear();
205 return shape(font, characterSize, string, offset, scale);
206 }
207
214 ShapeResult reshape(Font& font, u32 characterSize, std::u8string_view string, vec2 offset = {0.0f, 0.0f}, vec2 scale = {1.0f, 1.0f}) {
215 clear();
216 return shape(font, characterSize, string, offset, scale);
217 }
218
229 [[nodiscard]] std::span<const ShapedGlyph> getShapedGlyphs() const noexcept {
230 return shapedGlyphs;
231 }
232
243 [[nodiscard]] std::span<const ShapedGlyphInfo> getShapedGlyphsInfo() const noexcept {
244 return shapedGlyphsInfo;
245 }
246
257 [[nodiscard]] std::span<const ShapedLineInfo> getShapedLinesInfo() const noexcept {
258 return shapedLinesInfo;
259 }
260
267 [[nodiscard]] vec2 getMinExtent() const noexcept {
268 return minExtent;
269 }
270
277 [[nodiscard]] vec2 getMaxExtent() const noexcept {
278 return maxExtent;
279 }
280
281private:
282 std::vector<ShapedGlyph> shapedGlyphs{};
283 std::vector<ShapedGlyphInfo> shapedGlyphsInfo{};
284 std::vector<ShapedLineInfo> shapedLinesInfo{};
285 vec2 minExtent{std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
286 vec2 maxExtent{-std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity()};
287};
288
289} // namespace donut::graphics
290
291#endif
Typeface describing an assortment of character glyphs that may be rendered on-demand into an expandin...
Definition Font.hpp:42
Facility for shaping text, according to a Font, into renderable glyphs.
Definition Text.hpp:19
void clear() noexcept
Erase all shaped glyphs and reset the text to an empty state.
Definition Text.hpp:137
Text() noexcept=default
Construct an empty text.
std::span< const ShapedLineInfo > getShapedLinesInfo() const noexcept
Get the list of ShapedLineInfo data for all shaped lines.
Definition Text.hpp:257
std::span< const ShapedGlyph > getShapedGlyphs() const noexcept
Get the list of ShapedGlyph data for all shaped glyphs.
Definition Text.hpp:229
std::span< const ShapedGlyphInfo > getShapedGlyphsInfo() const noexcept
Get the list of ShapedGlyphInfo data for all shaped glyphs.
Definition Text.hpp:243
ShapeResult shape(Font &font, u32 characterSize, std::string_view string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper overload of shape() that takes an arbitrary byte string and interprets it as UTF-8.
Definition Text.hpp:191
ShapeResult reshape(Font &font, u32 characterSize, std::u8string_view string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper function that is equivalent to clear() followed by shape().
Definition Text.hpp:214
ShapeResult reshape(Font &font, u32 characterSize, std::string_view string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper function that is equivalent to clear() followed by shape().
Definition Text.hpp:203
ShapeResult shape(Font &font, u32 characterSize, std::u8string_view 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...
Text(Font &font, u32 characterSize, std::string_view string, vec2 offset={0.0f, 0.0f}, vec2 scale={1.0f, 1.0f})
Helper overload of Text() that takes an arbitrary byte string and interprets it as UTF-8.
Definition Text.hpp:130
vec2 getMinExtent() const noexcept
Get the minimum extent of the shaped text.
Definition Text.hpp:267
vec2 getMaxExtent() const noexcept
Get the maximum extent of the shaped text.
Definition Text.hpp:277
Definition Buffer.hpp:7
Result of the shape() function.
Definition Text.hpp:67
std::size_t shapedLineOffset
Index, into the list returned by getShapedLinesInfo(), of the ShapedLineInfo corresponding to the fir...
Definition Text.hpp:82
std::size_t shapedGlyphOffset
Index, into the lists returned by getShapedGlyphs() and getShapedGlyphsInfo(), of the ShapedGlyph and...
Definition Text.hpp:76
Additional information about a single shaped glyph, including some data that is not strictly required...
Definition Text.hpp:43
vec2 shapedAdvance
Scaled offset to apply in order to advance to the next glyph position, including kerning.
Definition Text.hpp:45
std::size_t stringOffset
Byte offset in the input string of the first code unit that this glyph originated from.
Definition Text.hpp:47
vec2 shapedOffset
Scaled offset from the starting position to draw this glyph at, in pixels.
Definition Text.hpp:44
std::size_t shapedLineIndex
Index of the ShapedLineInfo corresponding to the line that this glyph is part of.
Definition Text.hpp:46
Data required to render a single shaped glyph relative to at any given starting position.
Definition Text.hpp:28
u32 characterSize
Character size that this glyph was shaped at.
Definition Text.hpp:32
Font * font
Non-owning read-only non-null pointer to the font used to shape this glyph.
Definition Text.hpp:29
vec2 shapedOffset
Scaled offset from the starting position to draw this glyph at, in pixels.
Definition Text.hpp:30
char32_t codePoint
Unicode code point of this glyph.
Definition Text.hpp:33
vec2 shapedSize
Scaled size of this glyph's rectangle, in pixels.
Definition Text.hpp:31
Information about a line of shaped glyphs, including some data that is not strictly required for simp...
Definition Text.hpp:57
std::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 Text.hpp:61
vec2 shapedOffset
Scaled offset of the baseline at the start of this line of text.
Definition Text.hpp:58
vec2 shapedSize
Scaled total size of this line.
Definition Text.hpp:59
std::size_t shapedGlyphOffset
Index of the ShapedGlyph and ShapedGlyphInfo corresponding to the first glyph that is part of this li...
Definition Text.hpp:60