MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Text.cpp
Go to the documentation of this file.
1#include "Text.hpp"
2
3#include "GlyphAtlas.hpp"
4#include "TypeFaceFoundry.hpp"
5
6#include "FontDiscovery.hpp"
7
9
11
12namespace MayaFlux::Portal::Text {
13
14namespace {
15 bool g_initialized {};
16}
17
18bool initialize(std::optional<Core::TextConfig> config)
19{
20 if (g_initialized) {
22 "Portal::Text already initialized");
23 return true;
24 }
25
27 "Initializing Portal::Text...");
28
31 "Failed to initialize TypeFaceFoundry");
32 return false;
33 }
34
35 if (config) {
36 const auto& [family, style, pixel_size, atlas_size] = *config;
37 if (!set_default_font(family, style, pixel_size, atlas_size)) {
39 "Failed to set default font '{}{}{}'", family, style.empty() ? "" : " ", style);
40 }
41 } else {
43 "No default font configured for Portal::Text");
44 }
45
46 g_initialized = true;
48 "Portal::Text initialized");
49 return true;
50}
51
52std::shared_ptr<LayoutResult> create_layout(
53 std::string_view text,
54 float pen_x,
55 float pen_y,
56 uint32_t wrap_w)
57{
58 return std::make_shared<LayoutResult>(lay_out(text, get_default_atlas(), pen_x, pen_y, wrap_w));
59}
60
62{
63 if (!g_initialized) {
64 return;
65 }
66
68 "Shutting down Portal::Text...");
69
71
72 g_initialized = false;
74 "Portal::Text shutdown");
75}
76
78{
79 return g_initialized;
80}
81
83 std::string_view family,
84 std::string_view style,
85 uint32_t pixel_size,
86 uint32_t atlas_size)
87{
88 const auto path = find_font(family, style);
89 if (!path) {
91 "set_default_font: could not locate '{}{}{}' on this system",
92 family,
93 style.empty() ? "" : " ",
94 style);
95 return false;
96 }
97 return set_default_font(*path, pixel_size, atlas_size);
98}
99
100bool set_default_font(const std::string& path, uint32_t pixel_size, uint32_t atlas_size)
101{
102 return TypeFaceFoundry::instance().set_default_font(path, pixel_size, atlas_size);
103}
104
106{
109 atlas != nullptr, "call set_default_font before get_default_atlas");
110 return *atlas;
111}
112
113} // namespace MayaFlux::Portal::Text
#define MF_ASSERT(comp, ctx, condition,...)
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
Rasterizes and packs glyphs from a FontFace into a TextureContainer.
GlyphAtlas * get_default_glyph_atlas() const
Return the default GlyphAtlas, or nullptr if set_default_font() has not been called successfully.
bool set_default_font(const std::string &path, uint32_t pixel_size, uint32_t atlas_size=512)
Locate a system font by family and style, then load it as the default.
void shutdown()
Release the FreeType library handle.
void initialize()
Definition main.cpp:11
@ Init
Engine/subsystem initialization.
@ API
API calls from external code.
@ Portal
High-level user-facing API layer.
MAYAFLUX_API std::optional< std::string > find_font(std::string_view family, std::string_view style={})
Locate a system font file by family name and optional style.
bool is_initialized()
Returns true after a successful initialize() call.
Definition Text.cpp:77
LayoutResult lay_out(std::string_view text, GlyphAtlas &atlas, float pen_x, float pen_y, uint32_t wrap_w)
Lay out a UTF-8 string into a sequence of screen-space quads.
Definition TypeSetter.cpp:9
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:105