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