Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
font_resources.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
7#include <cstdint>
8#include <iostream>
9#include <map>
10#include <algorithm>
11
12#include "boost/filesystem.hpp"
13#include <ft2build.h>
14#include FT_FREETYPE_H
15#include FT_GLYPH_H
16#include FT_TYPES_H
17#include FT_OUTLINE_H
18#include FT_RENDER_H
19
22
23namespace rendering_engine
24{
25/**
26* @struct GlyphIndex
27* @brief Opaque glyph identifier within a font.
28*/
30{
31 std::uint32_t index;
32};
33
34/**
35 * @struct GlyphMetrics
36 * @brief Metrics describing a single glyph and its atlas placement.
37 *
38 * Contains layout information required for text positioning
39 * and texture atlas sampling.
40 */
42{
43 // Atlas placement
44 uint32_t atlasX = 0;
45 uint32_t atlasY = 0;
46 uint32_t width = 0;
47 uint32_t height = 0;
48
49 // Layout
50 int32_t bearingX = 0;
51 int32_t bearingY = 0;
52 int32_t advanceX = 0;
53 int32_t padding = 0;
54};
55
56/**
57 * @struct FontMetrics
58 * @brief Global metrics describing font vertical layout.
59 */
61{
62 int32_t lineHeight;
63 int32_t ascender;
64 int32_t descender;
65};
66
67class ImageData;
68class TextRenderer;
69
70/**
71 * @class FontResources
72 * @brief Manages glyph data, font atlases, and font-level metrics.
73 *
74 * Responsible for loading glyphs on demand, generating texture atlases,
75 * and providing glyph and font metrics required for text layout and rendering.
76 *
77 * Instances are bound to a specific font and font size.
78 */
80{
81
82public:
83 /**
84 * @brief Constructs font resources from a font file.
85 * @param rrc Rendering resource context.
86 * @param textRenderer Owning text renderer.
87 * @param filepath Path to the font file.
88 * @param fontSize Font size in pixels.
89 */
90 FontResources(RenderResourceContext rrc, TextRenderer* textRenderer, std::string filepath, unsigned int const fontSize);
91 /**
92 * @brief Constructs font resources from in-memory font data.
93 * @param rrc Rendering resource context.
94 * @param textRenderer Owning text renderer.
95 * @param fontName Logical font name.
96 * @param fileBytes Font file data.
97 * @param fontSize Font size in pixels.
98 */
99 FontResources(RenderResourceContext rrc, TextRenderer* textRenderer, std::string fontName, std::vector<uint8_t> const& fileBytes, unsigned int const fontSize);
100
101 /**
102 * @brief Releases all font-related resources.
103 */
105
106 /**
107 * @brief Preloads glyphs for a continuous Unicode range.
108 * @param begin First Unicode code point.
109 * @param end Last Unicode code point.
110 */
111 void LoadGlyphsFromCodePointRange(std::uint32_t begin, std::uint32_t end);
112
113 /**
114 * @brief Ensures glyphs for the given Unicode code points exist.
115 * @param codePoints List of Unicode code points.
116 */
117 void EnsureGlyphs(const std::vector<std::uint32_t>& codePoints);
118
119 /**
120 * @brief Ensures glyphs for the given glyph indices exist.
121 * @param glyphIndexes List of glyph indices.
122 */
123 void EnsureGlyphs(const std::vector<GlyphIndex>& glyphIndexes);
124
125 /**
126 * @brief Enables or disables storing generated font atlases on disk.
127 * @param in True to store atlases in files.
128 */
129 void StoreFontAtlasesInFiles(bool in);
130
131 /**
132 * @brief Returns global font metrics.
133 * @return Font metrics.
134 */
135 const FontMetrics& GetFontMetrics() const;
136
137 /**
138 * @brief Returns metrics for a specific glyph.
139 * @param glyphIndex Glyph identifier.
140 * @return Glyph metrics or default-initialized metrics if unavailable.
141 */
142 GlyphMetrics GetGlyphMetrics(GlyphIndex glyphIndex) const;
143
144 /**
145 * @brief Returns the texture name of the atlas containing the glyph.
146 * @param glyphIndex Glyph identifier.
147 * @return Texture name or empty string if unavailable.
148 */
149 std::string GetFontAtlasTextureName(GlyphIndex glyphIndex) const;
150
151 /**
152 * @brief Returns the material name associated with the glyph.
153 * @param glyphIndex Glyph identifier.
154 * @return Material name or empty string if unavailable.
155 */
156 std::string GetFontAtlasMaterialName(GlyphIndex glyphIndex) const;
157
158 /**
159 * @brief Resolves a Unicode code point to a glyph index.
160 * @param codePoint Unicode code point.
161 * @return Glyph index.
162 */
163 GlyphIndex GetIndexFromCodePoint(std::uint32_t codePoint) const;
164
165 /**
166 * @brief Returns the internal font face handle.
167 * @note Intended for internal use only.
168 */
169 FT_Face GetFontFace();
170
171protected:
172 /**
173 * @brief Creates a bitmap and metrics for a glyph by index.
174 * @param glyphIndex Glyph identifier.
175 * @return Pair of glyph metrics and image data.
176 */
177 std::pair<GlyphMetrics, ImageData> CreateGlyphBitmapBy(GlyphIndex glyphIndex);
178
179 /**
180 * @brief Creates a bitmap and metrics for a glyph by code point.
181 * @param codePoint Unicode code point.
182 * @return Pair of glyph metrics and image data.
183 */
184 std::pair<GlyphMetrics, ImageData> CreateGlyphBitmapBy(std::uint32_t codePoint);
185
186 /**
187 * @brief Creates a font atlas for a list of glyphs.
188 * @param glyphIndexes List of glyph indices.
189 */
190 void CreateFontAtlasFromList(const std::vector<GlyphIndex>& glyphIndexes);
191
192 /**
193 * @brief Creates a font atlas for a continuous code point range.
194 * @param begin First Unicode code point.
195 * @param end Last Unicode code point.
196 */
197 void CreateFontAtlasFromRange(std::uint32_t begin, std::uint32_t end);
198
199 /**
200 * @brief Checks whether a glyph exists for a code point.
201 * @param codePoint Unicode code point.
202 * @return True if the glyph exists.
203 */
204 bool HasGlyph(uint32_t codePoint) const;
205
206private:
208 FontResources operator=(FontResources const&);
209
210protected:
213 std::string mFontName;
214 const unsigned int mFontSize;
215
216 FT_Error mErrorResult = FT_Err_Ok;
217 FT_Face mFace = 0;
218
220
221 // Font atlases stored as unordered map where:
222 // key - std::uint32_t - glyph's corresponding index
223 // value - GlyphMetrics - metrics belong to particular glyph
224 // - std::string - material name, to which TextBlock can refer to.
225 std::unordered_map<std::uint32_t, std::pair<GlyphMetrics, std::string>> mGlyphsByIndex;
226
227 // Next container store names of created material name and corresponding font atlas textures name
228 std::unordered_map<std::string, std::string> mFontAtlases;
229
230 std::vector<uint8_t> mFontFileBytes;
231
232 bool bStoreFontAtlasesInFiles = false;
233
234 /// Maximum number of glyphs per font atlas.
235 static std::uint32_t sMaxGlyphsPerFontAtlas;
236 /// Padding in pixels around glyphs inside atlases.
237 static unsigned int sFontAtlasPaddingPx;
238};
239} // namespace rendering_engine
Manages glyph data, font atlases, and font-level metrics.
static std::uint32_t sMaxGlyphsPerFontAtlas
Maximum number of glyphs per font atlas.
std::unordered_map< std::uint32_t, std::pair< GlyphMetrics, std::string > > mGlyphsByIndex
static unsigned int sFontAtlasPaddingPx
Padding in pixels around glyphs inside atlases.
std::vector< uint8_t > mFontFileBytes
RenderResourceContext mRenderResourceContext
std::unordered_map< std::string, std::string > mFontAtlases
Represents raw 2D image data stored in memory.
Definition: image_data.hpp:80
Central text system manager and font resource registry.
#define RE_API
Global metrics describing font vertical layout.
Opaque glyph identifier within a font.
Metrics describing a single glyph and its atlas placement.
Aggregates pointers to global rendering resource managers.