MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TypeFaceFoundry.cpp
Go to the documentation of this file.
1#include "TypeFaceFoundry.hpp"
2
5
7
9
11
13
15{
16 if (m_library) {
18 "TypeFaceFoundry already initialized");
19 return true;
20 }
21
22 if (const FT_Error err = FT_Init_FreeType(&m_library); err != 0) {
24 "FT_Init_FreeType failed with error {}", static_cast<int>(err));
25 m_library = nullptr;
26 return false;
27 }
28
30 "TypeFaceFoundry initialized");
31 return true;
32}
33
34bool TypeFaceFoundry::set_default_font(const std::string& font_path, uint32_t pixel_size, uint32_t atlas_size)
35{
36 auto face = std::make_unique<FontFace>();
37 if (!face->load(font_path)) {
39 "set_default_font: failed to load '{}'", font_path);
40 return false;
41 }
42
43 auto atlas = std::make_unique<GlyphAtlas>(*face, pixel_size, atlas_size);
44 for (const auto& fallback : m_fallback_faces) {
45 if (fallback && fallback->is_loaded()) {
46 atlas->add_fallback(*fallback);
47 }
48 }
49
50 m_default_atlas = std::move(atlas);
51 m_default_face = std::move(face);
52
54 "Default font set: '{}' {}px atlas {}px", font_path, pixel_size, atlas_size);
55 return true;
56}
57
58bool TypeFaceFoundry::add_fallback_font(const std::string& font_path)
59{
60 if (!m_default_atlas) {
62 "add_fallback_font: call set_default_font first");
63 return false;
64 }
65
66 auto face = std::make_unique<FontFace>();
67 if (!face->load(font_path)) {
69 "add_fallback_font: failed to load '{}'", font_path);
70 return false;
71 }
72
73 m_default_atlas->add_fallback(*face);
74 m_fallback_faces.push_back(std::move(face));
75
77 "Fallback font added: '{}'", font_path);
78 return true;
79}
80
82{
83 if (!m_library) {
84 return;
85 }
86
87 if (m_default_face) {
88 m_default_face->unload();
89 }
90 for (const auto& fallback : m_fallback_faces) {
91 if (fallback) {
92 fallback->unload();
93 }
94 }
95
96 FT_Done_FreeType(m_library);
97 m_library = nullptr;
98
100 "TypeFaceFoundry shutdown");
101}
102
103} // namespace MayaFlux::Portal::Text
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
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.
std::unique_ptr< FontFace > m_default_face
std::unique_ptr< GlyphAtlas > m_default_atlas
std::vector< std::unique_ptr< FontFace > > m_fallback_faces
void shutdown()
Release the FreeType library handle.
bool initialize()
Initialise the FreeType library.
bool add_fallback_font(const std::string &font_path)
Load a font file and register it as a fallback on the default atlas.
@ API
API calls from external code.
@ Portal
High-level user-facing API layer.