MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextRun.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "GlyphMesh.hpp"
4
6
7/**
8 * @class TextRun
9 * @brief One string laid out as glyph quads, ready to draw as vertices.
10 *
11 * Owns the text, its PressParams and the layout, and turns them into
12 * triangle list vertices through a GlyphMap, so the same run can draw in NDC,
13 * a world plane or any other space. It is the vertex counterpart of press():
14 * no pixels are rasterized, the glyphs are drawn from the atlas image (see
15 * AtlasImage) as coverage.
16 *
17 * Layout runs from the pen origin press() uses for a VKImage: x at zero,
18 * baseline at the atlas ascender, wrapping at render_bounds.x. Of PressParams
19 * it reads atlas, color, spans, background and render_bounds; budget_h has no
20 * meaning here. A background with alpha above zero is drawn as one opaque flat
21 * quad over render_bounds beneath the glyphs.
22 *
23 * The layout bakes in atlas UVs. When the atlas is replaced or grows, the run
24 * is stale; vertices() lays out again first, so its output is always current.
25 * The caller binds the atlas image at the slot it passes.
26 */
27class MAYAFLUX_API TextRun {
28public:
29 explicit TextRun(PressParams params = {});
30
31 /// @brief Replace the text and lay it out.
32 void set(std::string_view text);
33
34 /// @brief Replace the parameters and lay the current text out again.
35 void set_params(PressParams params);
36
37 /// @brief Replace text and parameters together, laying out once.
38 void assign(std::string_view text, PressParams params);
39
40 /**
41 * @brief The atlas the run draws from: params().atlas, else the default.
42 * @return Null when none is available.
43 */
44 [[nodiscard]] GlyphAtlas* atlas() const;
45
46 [[nodiscard]] const std::string& text() const noexcept { return m_text; }
47 [[nodiscard]] const PressParams& params() const noexcept { return m_params; }
48
49 /// @brief The layout of the current text, in pixels with y down.
50 [[nodiscard]] const LayoutResult& layout() const noexcept { return m_layout; }
51
52 /**
53 * @brief True when the atlas the layout was built against has since been
54 * replaced or has grown, so its UVs no longer match.
55 */
56 [[nodiscard]] bool stale() const;
57
58 /**
59 * @brief Size in pixels the run is drawn at.
60 *
61 * render_bounds.x wide and at least one line high, so a region shorter
62 * than a line shows its text squeezed to fit rather than spilling past it.
63 * Fit the region onto this, not onto render_bounds.
64 */
65 [[nodiscard]] glm::vec2 extent() const;
66
67 /**
68 * @brief Height in pixels the text occupies, from the top of the first line
69 * to the bottom of the last.
70 */
71 [[nodiscard]] float content_height() const;
72
73 /**
74 * @brief Vertices for the background, if any, then every glyph.
75 *
76 * @param map Layout pixels to output space.
77 * @param slot Atlas image slot in the shader's texture array.
78 */
79 [[nodiscard]] std::vector<Kakshya::MeshVertex> vertices(const GlyphMap& map, uint32_t slot);
80
81 /**
82 * @brief Pixel rectangle of a caret at a byte offset of the text.
83 *
84 * Spans one line height on the line the offset falls on, in layout pixels
85 * with y down, so min.y is the top.
86 *
87 * @param byte_offset Codepoint boundary in the text.
88 * @param width_px Caret width in pixels.
89 */
90 [[nodiscard]] Kinesis::AABB2D caret(size_t byte_offset, float width_px);
91
92 /**
93 * @brief Byte offset of the codepoint boundary nearest a layout pixel.
94 *
95 * @param px Point in layout pixels with y down.
96 */
97 [[nodiscard]] size_t index_at(glm::vec2 px);
98
99private:
100 void lay_out_text();
101
103 std::string m_text;
105 const GlyphAtlas* m_atlas { nullptr };
106 uint32_t m_atlas_size { 0 };
107};
108
109} // namespace MayaFlux::Portal::Text
uint32_t px
Rasterizes and packs glyphs from a FontFace into a TextureContainer.
const LayoutResult & layout() const noexcept
The layout of the current text, in pixels with y down.
Definition TextRun.hpp:50
const PressParams & params() const noexcept
Definition TextRun.hpp:47
const std::string & text() const noexcept
Definition TextRun.hpp:46
One string laid out as glyph quads, ready to draw as vertices.
Definition TextRun.hpp:27
size_t index_at(std::string_view text, const LayoutResult &layout, GlyphAtlas &atlas, float x, float y, float pen_x, float pen_y, uint32_t wrap_w, uint32_t tab_width)
Byte offset of the codepoint boundary nearest a screen-space point.
Axis-aligned bounding rectangle in a 2D coordinate space.
Definition Bounds.hpp:63
Linear map from layout pixels to whatever space the vertices are drawn in.
Definition GlyphMesh.hpp:21
Result of lay_out(), carrying the quads and the final pen position.
Construction parameters for press().
Definition InkPress.hpp:65