MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TypeFaceFoundry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ft2build.h>
4#include FT_FREETYPE_H
5
7
8class FontFace;
9class GlyphAtlas;
10
11/**
12 * @class TypeFaceFoundry
13 * @brief Singleton owner of the FT_Library handle.
14 *
15 * Initialised once by Portal::Text::initialize() and shut down by
16 * Portal::Text::shutdown(). All FontFace instances borrow the library
17 * handle via get_library(); they must not outlive this singleton.
18 */
19class MAYAFLUX_API TypeFaceFoundry {
20public:
22 {
23 static TypeFaceFoundry ctx;
24 return ctx;
25 }
26
31
32 /**
33 * @brief Initialise the FreeType library.
34 * @return true on success, false if FT_Init_FreeType fails.
35 */
36 bool initialize();
37
38 /**
39 * @brief Release the FreeType library handle.
40 */
41 void shutdown();
42
43 /**
44 * @brief Returns true after a successful initialize() call.
45 */
46 [[nodiscard]] bool is_initialized() const { return m_library != nullptr; }
47
48 /**
49 * @brief Raw FT_Library handle for use by FontFace.
50 * @pre is_initialized() must be true.
51 */
52 [[nodiscard]] FT_Library get_library() const { return m_library; }
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 */
67 bool set_default_font(const std::string& path, uint32_t pixel_size, uint32_t atlas_size = 512);
68
69 /**
70 * @brief Return the default GlyphAtlas, or nullptr if set_default_font()
71 * has not been called successfully.
72 */
73 [[nodiscard]] GlyphAtlas* get_default_glyph_atlas() const { return m_default_atlas.get(); }
74
75 /**
76 * @brief Return the default FontFace, or nullptr if set_default_font() has
77 * not been called successfully.
78 */
79 [[nodiscard]] FontFace* get_default_face() const { return m_default_face.get(); }
80
81 /**
82 * @brief Load a font file and register it as a fallback on the default atlas.
83 *
84 * Tried, in registration order, whenever the default atlas's primary
85 * face lacks a codepoint. Requires set_default_font() to have been
86 * called first. Fallback faces are retained across a later
87 * set_default_font() call and re-registered on whatever new atlas that
88 * call constructs, so a font switch doesn't silently drop them.
89 *
90 * @param font_path Path to a TTF or OTF file.
91 * @return true on success.
92 */
93 bool add_fallback_font(const std::string& font_path);
94
95private:
98
99 FT_Library m_library { nullptr };
100
101 std::unique_ptr<FontFace> m_default_face;
102 std::unique_ptr<GlyphAtlas> m_default_atlas;
103 std::vector<std::unique_ptr<FontFace>> m_fallback_faces;
104};
105
106} // namespace MayaFlux::Portal::Text
Owns a single FT_Face loaded from a file path.
Definition FontFace.hpp:24
Rasterizes and packs glyphs from a FontFace into a TextureContainer.
FontFace * get_default_face() const
Return the default FontFace, or nullptr if set_default_font() has not been called successfully.
GlyphAtlas * get_default_glyph_atlas() const
Return the default GlyphAtlas, or nullptr if set_default_font() has not been called successfully.
TypeFaceFoundry & operator=(TypeFaceFoundry &&)=delete
bool is_initialized() const
Returns true after a successful initialize() call.
std::unique_ptr< FontFace > m_default_face
std::unique_ptr< GlyphAtlas > m_default_atlas
TypeFaceFoundry(const TypeFaceFoundry &)=delete
std::vector< std::unique_ptr< FontFace > > m_fallback_faces
TypeFaceFoundry & operator=(const TypeFaceFoundry &)=delete
TypeFaceFoundry(TypeFaceFoundry &&)=delete
FT_Library get_library() const
Raw FT_Library handle for use by FontFace.
Singleton owner of the FT_Library handle.
void initialize()
Definition main.cpp:10
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