Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
text_renderer.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5#pragma once
6
9#include "utility.hpp"
10
11#include <memory>
12#include <unordered_map>
13#include <cstdint>
14#include <string>
15#include <vector>
16
17#include <ft2build.h>
18#include FT_FREETYPE_H
19#include FT_GLYPH_H
20#include FT_TYPES_H
21#include FT_OUTLINE_H
22#include FT_RENDER_H
23
24namespace rendering_engine
25{
26class FontResources;
27
28/**
29 * @class TextRenderer
30 * @brief Central text system manager and font resource registry.
31 *
32 * Owns the font backend instance, manages font discovery and caching,
33 * defines script metadata, and coordinates font atlas preloading
34 * based on application configuration.
35 */
37{
38public:
39 /**
40 * @brief Constructs the text renderer and initializes internal font systems.
41 * @param rrc Rendering resource context.
42 *
43 * This object owns the lifetime of the font backend and must outlive
44 * all font resources created through it.
45 */
47 /**
48 * @brief Shuts down the text system and releases font backend resources.
49 *
50 * All FontResources obtained from this renderer must be destroyed
51 * before the renderer itself.
52 */
54 /**
55 * @brief Loads fonts from a filesystem folder.
56 * @param pathToFolder Path to the folder containing font files.
57 */
58 void LoadFontsFromFolder(std::string pathToFolder);
59 /**
60 * @brief Loads fonts bundled inside an application package.
61 */
62 void LoadFontsFromPackage();
63 /**
64 * @brief Returns the rendering resource context.
65 * @return Reference to the rendering resource context.
66 */
67 const RenderResourceContext& GetRenderResourceContext() const;
68 /**
69 * @brief Retrieves or creates font resources for a font and size.
70 *
71 * If the requested font resources are not yet loaded, they will be
72 * created on demand from available font sources.
73 *
74 * @param fontName Font family name.
75 * @param fontSize Font size in pixels.
76 * @return Shared pointer to font resources, or nullptr if unavailable.
77 */
78 std::shared_ptr<FontResources> GetFontResources(const std::string& fontName, int fontSize);
79 /**
80 * @brief Enables or disables storing generated font atlases to files.
81 * @param in True to store atlases on disk.
82 */
83 void StoreFontAtlasesInFiles(bool in);
84 /**
85 * @brief Returns scripts that require shaping before rendering.
86 * @return List of script identifiers.
87 */
88 const std::vector<std::string>& GetScriptsRequiredShaping() const;
89 /**
90 * @brief Returns the Unicode codepoint range for a script.
91 * @param script Script identifier.
92 * @return Pair of <begin, end> Unicode codepoints.
93 */
94 std::pair<std::uint32_t, std::uint32_t> GetScriptRange(std::string script);
95 /**
96 * @brief Returns the internal FreeType library instance.
97 * @return Reference to FT_Library.
98 */
99 inline FT_Library& GetFontLibrary()
100 {
101 return mLibrary;
102 }
103
104 void Shutdown();
105
106protected:
107 /**
108 * @brief Discovers fonts available in a filesystem folder.
109 * @param pathToFolder Path to the font folder.
110 */
111 void LoadFontsAvailableInFolder(std::string pathToFolder);
112 /**
113 * @brief Loads preloadable font atlases from a folder.
114 * @param availableFontsInFolder Map of font names to file paths.
115 */
116 void LoadPreloadableFontAtlasesFromFolder(const std::unordered_map<std::string, std::string>& availableFontsInFolder);
117 /**
118 * @brief Discovers fonts available in the application package.
119 */
120 void LoadFontsAvailableInPackage();
121 /**
122 * @brief Loads preloadable font atlases from the application package.
123 * @param availableFontsInPackage Map of font names to package paths.
124 */
125 void LoadPreloadableFontAtlasesFromPackage(const std::unordered_map<std::string, std::string>& availableFontsInPackage);
126private:
127 struct PairHash
128 {
129 std::size_t operator()(const std::pair<std::string, int>& p) const {
130 return std::hash<std::string>{}(p.first) ^ (std::hash<int>{}(p.second) << 1);
131 }
132 };
133
134 FT_Library mLibrary = 0;
135 FT_Error mErrorResult = FT_Err_Ok;
136
137 RenderResourceContext mRenderResourceContext;
138
139 // Store name of fonts avalable from font's folder
140 std::unordered_map<std::string, std::string> mAvailableFontsInFolder;
141 std::unordered_map<std::string, std::string> mAvailableFontsInPackage;
142
143 std::unordered_map<std::pair<std::string, int>, std::shared_ptr<FontResources>, PairHash> mFontResources;
144
145 bool bStoreFontAtlasesInFiles = false;
146
147 /**
148 * @brief Unicode script ranges known to the text system.
149 *
150 * Used for script detection, shaping decisions, and glyph preloading.
151 */
152 static std::unordered_map<std::string, std::pair<std::uint32_t, std::uint32_t>> sScriptRanges;
153 /**
154 * @brief Scripts that require text shaping during layout.
155 */
156 static std::vector<std::string> sScriptsRequiresShaping;
157 /**
158 * @brief Scripts whose glyphs can be safely preloaded without shaping.
159 */
160 static std::vector<std::string> sFontAtlasPreloadableScripts;
161};
162
163} // namespace rendering_engine
164
Central text system manager and font resource registry.
FT_Library & GetFontLibrary()
Returns the internal FreeType library instance.
#define RE_API
Aggregates pointers to global rendering resource managers.