Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
rendering_engine::TextRenderer Class Reference

Central text system manager and font resource registry. More...

#include <text_renderer.hpp>

Public Member Functions

 TextRenderer (RenderResourceContext rrc)
 Constructs the text renderer and initializes internal font systems.
 
 ~TextRenderer ()
 Shuts down the text system and releases font backend resources.
 
void LoadFontsFromFolder (std::string pathToFolder)
 Loads fonts from a filesystem folder.
 
void LoadFontsFromPackage ()
 Loads fonts bundled inside an application package.
 
const RenderResourceContext & GetRenderResourceContext () const
 Returns the rendering resource context.
 
std::shared_ptr< FontResources > GetFontResources (const std::string &fontName, unsigned int fontSize)
 Retrieves or creates font resources for a font and size.
 
void StoreFontAtlasesInFiles (bool in)
 Enables or disables storing generated font atlases to files.
 
const std::vector< std::string > & GetScriptsRequiredShaping () const
 Returns scripts that require shaping before rendering.
 
std::pair< std::uint32_t, std::uint32_t > GetScriptRange (std::string script)
 Returns the Unicode codepoint range for a script.
 
FT_Library & GetFontLibrary ()
 Returns the internal FreeType library instance.
 
void Shutdown ()
 

Protected Member Functions

void LoadFontsAvailableInFolder (std::string pathToFolder)
 Discovers fonts available in a filesystem folder.
 
void LoadPreloadableFontAtlasesFromFolder (const std::unordered_map< std::string, std::string > &availableFontsInFolder)
 Loads preloadable font atlases from a folder.
 
void LoadFontsAvailableInPackage ()
 Discovers fonts available in the application package.
 
void LoadPreloadableFontAtlasesFromPackage (const std::unordered_map< std::string, std::string > &availableFontsInPackage)
 Loads preloadable font atlases from the application package.
 

Detailed Description

Central text system manager and font resource registry.

Owns the font backend instance, manages font discovery and caching, defines script metadata, and coordinates font atlas preloading based on application configuration.

Definition at line 36 of file text_renderer.hpp.

Constructor & Destructor Documentation

◆ TextRenderer()

rendering_engine::TextRenderer::TextRenderer ( RenderResourceContext  rrc)

Constructs the text renderer and initializes internal font systems.

Parameters
rrcRendering resource context.

This object owns the lifetime of the font backend and must outlive all font resources created through it.

Definition at line 95 of file text_renderer.cpp.

96 :
97 mRenderResourceContext(rrc)
98{
99 LOG_INFO("Initializing TextRenderer...");
100 auto start = std::chrono::steady_clock::now();
101
102 mErrorResult = FT_Init_FreeType(&mLibrary);
103 if (mErrorResult)
104 {
105 LOG_ERROR("Failed to initialize FreeType library!");
106 throw std::runtime_error{ "Failed to initialize FreeType library!" };
107 }
108
109 auto end = std::chrono::steady_clock::now();
110 float ms = std::chrono::duration<float, std::milli>(end - start).count();
111
112 LOG_INFO("FreeType initialized in " + std::to_string(ms) + " ms.");
113}
#define LOG_ERROR(msg)
Definition logger.hpp:41
#define LOG_INFO(msg)
Definition logger.hpp:39

◆ ~TextRenderer()

rendering_engine::TextRenderer::~TextRenderer ( )

Shuts down the text system and releases font backend resources.

All FontResources obtained from this renderer must be destroyed before the renderer itself.

Definition at line 115 of file text_renderer.cpp.

116{
117}

Member Function Documentation

◆ GetFontLibrary()

FT_Library & rendering_engine::TextRenderer::GetFontLibrary ( )
inline

Returns the internal FreeType library instance.

Returns
Reference to FT_Library.

Definition at line 99 of file text_renderer.hpp.

100 {
101 return mLibrary;
102 }

◆ GetFontResources()

std::shared_ptr< FontResources > rendering_engine::TextRenderer::GetFontResources ( const std::string &  fontName,
unsigned int  fontSize 
)

Retrieves or creates font resources for a font and size.

If the requested font resources are not yet loaded, they will be created on demand from available font sources.

Parameters
fontNameFont family name.
fontSizeFont size in pixels.
Returns
Shared pointer to font resources, or nullptr if unavailable.

Definition at line 159 of file text_renderer.cpp.

160{
161 auto key = std::make_pair(fontName, fontSize);
162 if (auto search = mFontResources.find(key); search != mFontResources.end())
163 {
164 return search->second;
165 }
166 else
167 {
168 if (!mAvailableFontsInPackage.empty())
169 {
170 if (!mAvailableFontsInPackage.empty())
171 {
172 auto foundInPackage = mAvailableFontsInPackage.find(fontName);
173 if (foundInPackage != mAvailableFontsInPackage.end())
174 {
175 const std::string virtualFilePath = mAvailableFontsInPackage[fontName];
176 std::vector<uint8_t> binaryFileData = Utility::ReadPackedFile(virtualFilePath);
177
178 mFontResources[key] = std::make_shared<FontResources>(mRenderResourceContext, this, fontName, binaryFileData, fontSize);
179 mFontResources[key]->StoreFontAtlasesInFiles(bStoreFontAtlasesInFiles);
180
181 return mFontResources[key];
182 }
183 else
184 {
185 // Log Font {fontName} is not avalable in asset package
186 }
187 }
188 }
189 else
190 {
191 if (!mAvailableFontsInFolder.empty())
192 {
193 auto foundInFolder = mAvailableFontsInFolder.find(fontName);
194 if (foundInFolder != mAvailableFontsInFolder.end())
195 {
196 mFontResources[key] = std::make_shared<FontResources>(mRenderResourceContext, this, mAvailableFontsInFolder[fontName], fontSize);
197
198 return mFontResources[key];
199 }
200 else
201 {
202 // Log Font {fontName} is not avalable in asset folder
203 }
204 }
205 }
206 }
207
208 return nullptr;
209}
static std::vector< uint8_t > ReadPackedFile(const std::string &entryPath)
Reads raw bytes of a file stored inside Pack.bin.
Definition utility.cpp:331

◆ GetRenderResourceContext()

const RenderResourceContext & rendering_engine::TextRenderer::GetRenderResourceContext ( ) const

Returns the rendering resource context.

Returns
Reference to the rendering resource context.

Definition at line 154 of file text_renderer.cpp.

155{
156 return mRenderResourceContext;
157}

◆ GetScriptRange()

std::pair< std::uint32_t, std::uint32_t > rendering_engine::TextRenderer::GetScriptRange ( std::string  script)

Returns the Unicode codepoint range for a script.

Parameters
scriptScript identifier.
Returns
Pair of <begin, end> Unicode codepoints.

Definition at line 221 of file text_renderer.cpp.

222{
223 std::pair<std::uint32_t, std::uint32_t> result{ 0U, 0U };
224
225 auto search = sScriptRanges.find(script);
226 {
227 if (search != sScriptRanges.end())
228 return sScriptRanges[script];
229 }
230 return result;
231}

◆ GetScriptsRequiredShaping()

const std::vector< std::string > & rendering_engine::TextRenderer::GetScriptsRequiredShaping ( ) const

Returns scripts that require shaping before rendering.

Returns
List of script identifiers.

Definition at line 216 of file text_renderer.cpp.

217{
218 return sScriptsRequiresShaping;
219}

◆ LoadFontsAvailableInFolder()

void rendering_engine::TextRenderer::LoadFontsAvailableInFolder ( std::string  pathToFolder)
protected

Discovers fonts available in a filesystem folder.

Parameters
pathToFolderPath to the font folder.

Definition at line 233 of file text_renderer.cpp.

234{
235 LOG_INFO("Scanning font folder: " + pathToFolder);
236 // 1. Check if path is valid and exist
237 std::filesystem::path pathToDirectory = std::filesystem::path(pathToFolder);
238 const bool isValidFolderPath = std::filesystem::exists(std::filesystem::path(pathToFolder)) && std::filesystem::is_directory(std::filesystem::path(pathToFolder));
239 if (!isValidFolderPath)
240 {
241 return;
242 }
243 // 2. Iterate through files in the folder.
244 // if file is in the list of supported extensions
245 for (std::filesystem::recursive_directory_iterator it(pathToDirectory), end;
246 it != end;
247 ++it)
248 {
249 const std::filesystem::path& filePath = it->path();
250
251 if (!std::filesystem::is_regular_file(filePath))
252 continue;
253
254 const std::string ext = filePath.extension().string();
255 if (ext != ".ttf" && ext != ".otf")
256 continue;
257
258 // Fonts name and file path stored for future runtime loading
259 std::string fontName = filePath.stem().string();
260 mAvailableFontsInFolder[fontName] = filePath.string();
261 }
262 LOG_INFO("Discovered " +
263 std::to_string(mAvailableFontsInFolder.size()) +
264 " fonts in folder.");
265}

◆ LoadFontsAvailableInPackage()

void rendering_engine::TextRenderer::LoadFontsAvailableInPackage ( )
protected

Discovers fonts available in the application package.

Definition at line 308 of file text_renderer.cpp.

309{
310 LOG_INFO("Scanning fonts in package...");
311 const auto& entries = Utility::GetPackEntries();
312
313 std::string folderEntry = { "Fonts/" };
314 for (auto& entry : entries)
315 {
316 const std::string& virtualPath = entry.first;
317 if (virtualPath.rfind(folderEntry, 0) == 0) // starts with Fonts/
318 {
319 auto fontFilePath = std::filesystem::path(virtualPath);
320 const std::string ext = fontFilePath.extension().string();
321 if (ext != ".ttf" && ext != ".otf")
322 continue;
323
324 // Fonts name and virtual path stored for future runtime loading
325 std::string fontName = fontFilePath.stem().string();
326 mAvailableFontsInPackage[fontName] = fontFilePath.string();
327 }
328 }
329 LOG_INFO("Discovered " +
330 std::to_string(mAvailableFontsInPackage.size()) +
331 " fonts in package.");
332}
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
Definition utility.cpp:290

◆ LoadFontsFromFolder()

void rendering_engine::TextRenderer::LoadFontsFromFolder ( std::string  pathToFolder)

Loads fonts from a filesystem folder.

Parameters
pathToFolderPath to the folder containing font files.

Definition at line 135 of file text_renderer.cpp.

136{
137 LOG_INFO("Loading fonts from folder: " + pathToFolder);
138 auto start = std::chrono::steady_clock::now();
139 LoadFontsAvailableInFolder(pathToFolder);
140 LoadPreloadableFontAtlasesFromFolder(mAvailableFontsInFolder);
141 auto end = std::chrono::steady_clock::now();
142 float ms = std::chrono::duration<float, std::milli>(end - start).count();
143
144 LOG_INFO("Font discovery and preload complete in " +
145 std::to_string(ms) + " ms.");
146}
void LoadFontsAvailableInFolder(std::string pathToFolder)
Discovers fonts available in a filesystem folder.
void LoadPreloadableFontAtlasesFromFolder(const std::unordered_map< std::string, std::string > &availableFontsInFolder)
Loads preloadable font atlases from a folder.

◆ LoadFontsFromPackage()

void rendering_engine::TextRenderer::LoadFontsFromPackage ( )

Loads fonts bundled inside an application package.

Definition at line 148 of file text_renderer.cpp.

149{
151 LoadPreloadableFontAtlasesFromPackage(mAvailableFontsInPackage);
152}
void LoadFontsAvailableInPackage()
Discovers fonts available in the application package.
void LoadPreloadableFontAtlasesFromPackage(const std::unordered_map< std::string, std::string > &availableFontsInPackage)
Loads preloadable font atlases from the application package.

◆ LoadPreloadableFontAtlasesFromFolder()

void rendering_engine::TextRenderer::LoadPreloadableFontAtlasesFromFolder ( const std::unordered_map< std::string, std::string > &  availableFontsInFolder)
protected

Loads preloadable font atlases from a folder.

Parameters
availableFontsInFolderMap of font names to file paths.

Definition at line 267 of file text_renderer.cpp.

268{
269 LOG_INFO("Preloading font atlases from folder...");
270 auto start = std::chrono::steady_clock::now();
271 AppConfig appConfig = Utility::ReadConfigFile();
272
273 for (const auto& [fontName, filePath] : availableFontsInFolder)
274 {
275 for (auto requestedScript : appConfig.textScripts)
276 {
277 auto preloadableScriptIt = std::find(sFontAtlasPreloadableScripts.begin(), sFontAtlasPreloadableScripts.end(), requestedScript);
278 if (preloadableScriptIt != sFontAtlasPreloadableScripts.end())
279 {
280 for (auto fontSize : appConfig.fontSizePreload)
281 {
282 auto key = std::make_pair(fontName, fontSize);
283 if (mFontResources.find(key) == mFontResources.end())
284 {
285 LOG_DEBUG("Creating font resource: " + fontName +
286 " size " + std::to_string(fontSize));
287 mFontResources[key] = std::make_shared<FontResources>(mRenderResourceContext, this, filePath, fontSize);
288 mFontResources[key]->StoreFontAtlasesInFiles(bStoreFontAtlasesInFiles);
289 }
290 LOG_DEBUG("Preloading script '" + requestedScript +
291 "' for font " + fontName +
292 " size " + std::to_string(fontSize));
293 const std::uint32_t rangeBegin = sScriptRanges[requestedScript].first;
294 const std::uint32_t rangeEnd = sScriptRanges[requestedScript].second;
295 mFontResources[key]->LoadGlyphsFromCodePointRange(rangeBegin, rangeEnd);
296 }
297 }
298 }
299 }
300
301 auto end = std::chrono::steady_clock::now();
302 float ms = std::chrono::duration<float, std::milli>(end - start).count();
303
304 LOG_INFO("Font atlas preloading (folder) completed in " +
305 std::to_string(ms) + " ms.");
306}
static AppConfig ReadConfigFile()
Reads application settings from the JSON config file.
Definition utility.cpp:37
#define LOG_DEBUG(msg)
Definition logger.hpp:38

◆ LoadPreloadableFontAtlasesFromPackage()

void rendering_engine::TextRenderer::LoadPreloadableFontAtlasesFromPackage ( const std::unordered_map< std::string, std::string > &  availableFontsInPackage)
protected

Loads preloadable font atlases from the application package.

Parameters
availableFontsInPackageMap of font names to package paths.

Definition at line 334 of file text_renderer.cpp.

335{
336 LOG_INFO("Preloading font atlases from package...");
337 auto start = std::chrono::steady_clock::now();
338 AppConfig appConfig = Utility::ReadConfigFile();
339
340 for (const auto& [fontName, virtualFilePath] : availableFontsInPackage)
341 {
342 for (auto requestedScript : appConfig.textScripts)
343 {
344 auto preloadableScriptIt = std::find(sFontAtlasPreloadableScripts.begin(), sFontAtlasPreloadableScripts.end(), requestedScript);
345 if (preloadableScriptIt != sFontAtlasPreloadableScripts.end())
346 {
347 for (auto fontSize : appConfig.fontSizePreload)
348 {
349 auto key = std::make_pair(fontName, fontSize);
350 if (mFontResources.find(key) == mFontResources.end())
351 {
352 std::vector<uint8_t> binaryFileData = Utility::ReadPackedFile(virtualFilePath);
353 LOG_DEBUG("Creating font resource: " + fontName +
354 " size " + std::to_string(fontSize));
355 mFontResources[key] = std::make_shared<FontResources>(mRenderResourceContext, this, fontName, binaryFileData, fontSize);
356 mFontResources[key]->StoreFontAtlasesInFiles(bStoreFontAtlasesInFiles);
357 }
358 LOG_DEBUG("Preloading script '" + requestedScript +
359 "' for font " + fontName +
360 " size " + std::to_string(fontSize));
361 const std::uint32_t rangeBegin = sScriptRanges[requestedScript].first;
362 const std::uint32_t rangeEnd = sScriptRanges[requestedScript].second;
363 mFontResources[key]->LoadGlyphsFromCodePointRange(rangeBegin, rangeEnd);
364 }
365 }
366 }
367 }
368
369 auto end = std::chrono::steady_clock::now();
370 float ms = std::chrono::duration<float, std::milli>(end - start).count();
371
372 LOG_INFO("Font atlas preloading (package) completed in " +
373 std::to_string(ms) + " ms.");
374}

◆ Shutdown()

void rendering_engine::TextRenderer::Shutdown ( )

Definition at line 119 of file text_renderer.cpp.

120{
121 LOG_INFO("Shutting down TextRenderer...");
122 if (!mLibrary)
123 return;
124
125 for (auto& fontResource : mFontResources)
126 fontResource.second.reset();
127
128 mFontResources.clear();
129
130 FT_Done_FreeType(mLibrary);
131 mLibrary = nullptr;
132 LOG_INFO("TextRenderer shutdown complete.");
133}

◆ StoreFontAtlasesInFiles()

void rendering_engine::TextRenderer::StoreFontAtlasesInFiles ( bool  in)

Enables or disables storing generated font atlases to files.

Parameters
inTrue to store atlases on disk.

Definition at line 211 of file text_renderer.cpp.

212{
213 bStoreFontAtlasesInFiles = in;
214}

The documentation for this class was generated from the following files: