MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Text.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Core {
7struct TextConfig;
8}
9
10namespace MayaFlux::Portal::Text {
11
12class GlyphAtlas;
13
14/**
15 * @brief Initialize Portal::Text.
16 *
17 * Initialises TypeFaceFoundry. Must be called before constructing any
18 * FontFace or GlyphAtlas. Safe to call after Portal::Graphics::initialize().
19 *
20 * @return true on success.
21 */
22MAYAFLUX_API bool initialize(std::optional<Core::TextConfig> config);
23
24/**
25 * @brief Shutdown Portal::Text.
26 *
27 * Releases the FreeType library handle and destroys the default atlas if
28 * one was set. All FontFace and GlyphAtlas instances must be destroyed
29 * before calling this.
30 */
31MAYAFLUX_API void shutdown();
32
33/**
34 * @brief Returns true after a successful initialize() call.
35 */
36MAYAFLUX_API bool is_initialized();
37
38/**
39 * @brief Load a font file and create the default GlyphAtlas at a given size.
40 *
41 * Replaces any previously set default font. Must be called after
42 * initialize().
43 *
44 * @param font_path Path to a TTF or OTF file.
45 * @param pixel_size Glyph height in pixels.
46 * @param atlas_size Atlas texture dimension (power of two, default 512).
47 * @return true on success.
48 */
49MAYAFLUX_API bool set_default_font(
50 const std::string& font_path,
51 uint32_t pixel_size = 24,
52 uint32_t atlas_size = 512);
53
54/**
55 * @brief Locate a system font by family and style, then load it as the default.
56 *
57 * Delegates font path resolution to find_font, then calls the
58 * path-based overload. Returns false and logs if the family cannot be
59 * located on the current platform.
60 *
61 * @param family Font family name, e.g. "JetBrains Mono".
62 * @param style Style hint, e.g. "Medium", "Bold".
63 * @param pixel_size Glyph height in pixels.
64 * @param atlas_size Atlas texture dimension (power of two, default 512).
65 * @return true on success.
66 */
67MAYAFLUX_API bool set_default_font(
68 std::string_view family,
69 std::string_view style,
70 uint32_t pixel_size,
71 uint32_t atlas_size = 512);
72
73/**
74 * @brief Load a font file and register it as a fallback on the default atlas.
75 *
76 * Tried, in registration order, whenever the default font lacks a codepoint.
77 * Must be called after set_default_font(); survives a later call to
78 * set_default_font() (re-registered on the atlas it constructs).
79 *
80 * @param font_path Path to a TTF or OTF file.
81 * @return true on success.
82 */
83MAYAFLUX_API bool add_fallback_font(const std::string& font_path);
84
85/**
86 * @brief Locate a system font by family and style, then register it as a fallback.
87 *
88 * Delegates font path resolution to find_font, then calls the path-based
89 * overload.
90 *
91 * @param family Font family name, e.g. "Noto Sans CJK".
92 * @param style Style hint, e.g. "Regular".
93 * @return true on success.
94 */
95MAYAFLUX_API bool add_fallback_font(std::string_view family, std::string_view style);
96
97/**
98 * @brief Lay out a UTF-8 string into screen-space glyph quads using the default atlas.
99 *
100 * Each quad in the returned LayoutResult carries pixel-space position, atlas
101 * UV coordinates, and the Unicode codepoint that produced it. The caller may
102 * mutate the quads freely before passing them to rasterize_quads().
103 *
104 * @param text UTF-8 encoded input string.
105 * @param pen_x Starting horizontal pen position in pixels.
106 * @param pen_y Starting vertical pen position in pixels (baseline).
107 * @param wrap_w Wrap boundary in pixels. Zero disables wrapping.
108 * @return LayoutResult containing quads and final pen position.
109 * @pre set_default_font() must have been called successfully.
110 */
111[[nodiscard]] MAYAFLUX_API std::shared_ptr<LayoutResult> create_layout(
112 std::string_view text,
113 float pen_x = 0.F,
114 float pen_y = 0.F,
115 uint32_t wrap_w = 0);
116
117/**
118 * @brief Return the default GlyphAtlas, or nullptr if set_default_font()
119 * has not been called successfully.
120 */
121MAYAFLUX_API GlyphAtlas& get_default_atlas();
122
123} // namespace MayaFlux::Portal::Text
Text editing operations: a mutable buffer plus a byte-offset cursor, and the operations that keep it ...
void initialize()
Definition main.cpp:10
bool is_initialized()
Returns true after a successful initialize() call.
Definition Text.cpp:77
bool add_fallback_font(const std::string &font_path)
Load a font file and register it as a fallback on the default atlas.
Definition Text.cpp:105
bool set_default_font(std::string_view family, std::string_view style, uint32_t pixel_size, uint32_t atlas_size)
Locate a system font by family and style, then load it as the default.
Definition Text.cpp:82
void shutdown()
Shutdown Portal::Text.
Definition Text.cpp:61
std::shared_ptr< LayoutResult > create_layout(std::string_view text, float pen_x, float pen_y, uint32_t wrap_w)
Lay out a UTF-8 string into screen-space glyph quads using the default atlas.
Definition Text.cpp:52
GlyphAtlas & get_default_atlas()
Return the default GlyphAtlas, or nullptr if set_default_font() has not been called successfully.
Definition Text.cpp:124