Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Font.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_FONT
19#define INCLUDED_FONT
20
21#include "graphics/Texture.h"
23#include "lib/code_annotation.h"
24#include "lib/os_path.h"
25#include "maths/Rect.h"
27
28#include <array>
29#include <ft2build.h>
30#include FT_FREETYPE_H
31#include FT_GLYPH_H
32#include FT_IMAGE_H
33#include FT_STROKER_H
34#include <functional>
35#include <memory>
36#include <optional>
37#include <string>
38#include <type_traits>
39#include <vector>
40
41class CVector2D;
42namespace Renderer::Backend { class IDevice; }
43namespace Renderer::Backend { class IDeviceCommandContext; }
44namespace Renderer::Backend::Sampler { struct Desc; }
45
46/**
47 * Storage for a bitmap font. Loaded by CFontManager.
48 */
49class CFont
50{
51public:
52 CFont(FT_Library library, const std::array<float, 256>& gammaCorrection) : m_FreeType{library}, m_GammaCorrectionLUT{gammaCorrection} {}
53 ~CFont() = default;
56
57 struct GlyphData
58 {
59 float u0, v0, u1, v1;
60 float x0, y0, x1, y1;
61 float xadvance;
62 float yadvance;
63 std::uint8_t defined{0};
64 FT_Face face;
65 };
66
67 /**
68 * Relatively efficient lookup of GlyphData from 16-bit Unicode codepoint.
69 * This is stored as a sparse 2D array, exploiting the knowledge that a font
70 * typically only supports a small number of 256-codepoint blocks, so most
71 * elements of m_Data will be NULL.
72 * This implementation expand the store the GlyphData for a given Unicode codepoint (0 ≤ cp ≤ 0x10FFFF)
73 */
75 {
76 public:
77 GlyphMap() = default;
78 ~GlyphMap() = default;
81
82 /**
83 * @brief Store the GlyphData for a given Unicode codepoint
84 *
85 * @param codepoint The unicode codepoint (0 ≤ cp ≤ 0x10FFFF)
86 * @param glyph The glyphData data to store
87 */
88 void set(std::uint16_t codepoint, const GlyphData& glyph);
89
90 const GlyphData* get(std::uint16_t codepoint) const;
91 private:
92 std::unique_ptr<std::array<GlyphData, 256>> m_Data[256];
93 };
94
95 bool HasRGB() const { return m_HasRGB; }
96 float GetHeight() const;
97
98 /**
99 * In typography, cap height is the height of a capital letter above the baseline for a particular typeface.
100 * It specifically is the height of capital letters that are flat—such as H or I—as opposed to round letters such as O, or pointed letters like A,
101 * both of which may display overshoot. The height of the small letters is the x-height.
102 * REf: https://en.wikipedia.org/wiki/Cap_height
103 */
104 float GetCapHeight();
105
106 float GetCharacterWidth(wchar_t c);
107 float GetStrokeWidth() const { return m_StrokeWidth; }
108 void CalculateStringSize(const wchar_t* string, float& w, float& h);
109 void GetGlyphBounds(float& x0, float& y0, float& x1, float& y1) const
110 {
111 x0 = m_Bounds.left;
112 y0 = m_Bounds.top;
113 x1 = m_Bounds.right;
114 y1 = m_Bounds.bottom;
115 }
116
117 CTexturePtr GetTexture() const { return m_Texture; }
119 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
121 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
122 const GlyphData* GetGlyph(std::uint16_t i);
123
124private:
125 static void ftFaceDeleter(FT_Face face)
126 {
127 FT_Done_Face(face);
128 }
129
130 static void ftStrokerDeleter(FT_Stroker stroker)
131 {
132 FT_Stroker_Done(stroker);
133 }
134
135 using UniqueFTFace = std::unique_ptr<std::remove_pointer_t<FT_Face>, decltype(&ftFaceDeleter)>;
136 using UniqueFTStroker = std::unique_ptr<std::remove_pointer_t<FT_Stroker>, decltype(&ftStrokerDeleter)>;
137
138 friend class CFontManager;
139
140 bool AddFontFromPath(const OsPath& fontPath);
141 bool SetFontParams(
142 Renderer::Backend::IDevice* device, const std::string& fontName,
143 float size, float strokeWidth, float scale);
144
145 void BlendGlyphBitmapToTexture(const FT_Bitmap& bitmap, int targetX, int targetY, std::uint8_t r,
146 std::uint8_t g, std::uint8_t b);
147 void BlendGlyphBitmapToTextureRGBA(const FT_Bitmap& bitmap, int targetX, int targetY, std::uint8_t r,
148 std::uint8_t g, std::uint8_t b);
149 void BlendGlyphBitmapToTextureR8(const FT_Bitmap& bitmap, int targetX, int targetY);
150
151 std::optional<CVector2D> GenerateStrokeGlyphBitmap(const FT_Glyph& glyph, std::uint16_t codepoint,
152 FT_Render_Mode renderMode, const float baselineInAtlas);
153 std::optional<CVector2D> GenerateGlyphBitmap(FT_Glyph& glyph, std::uint16_t codepoint,
154 FT_Render_Mode renderMode, CVector2D offset, const float baselineInAtlas);
155
156 const GlyphData* ExtractAndGenerateGlyph(std::uint16_t codepoint);
159
161
162 // True if RGBA, false if ALPHA.
163 bool m_HasRGB{true};
164
166
167 // Height of a capital letter, roughly.
168 float m_Height;
169
170 // Bounding box of all glyphs
172
173 // The position for the next glyph in the texture atlas.
176
177 // Size of the texture atlas.
180
182 bool m_IsDirty{false};
184 float m_StrokeWidth{0.0f};
185 float m_Scale{1.0f};
186
187 std::unique_ptr<std::uint8_t[]> m_TexData;
191
192 float m_FontSize{0.0f};
193 std::string m_FontName;
194 std::reference_wrapper<const std::array<float, 256>> m_GammaCorrectionLUT;
195
196 FT_Library m_FreeType;
197 std::vector<std::unique_ptr<std::uint8_t[], AlignedDeleter>> m_FontsData;
198 std::vector<UniqueFTFace> m_Faces;
200
201 /**
202 * Some fonts are not rendered well at small sizes, so we set a minimum size.
203 * Because we are using a bitmap blending mode, when a font is using small size,
204 * We need to use a different render mode, one with less antialiasing.
205 */
206 static constexpr float MINIMAL_FONT_SIZE_ANTIALIASING{12.0f};
207};
208
209#endif // INCLUDED_FONT
Font manager: loads and caches bitmap fonts.
Definition: FontManager.h:41
Relatively efficient lookup of GlyphData from 16-bit Unicode codepoint.
Definition: Font.h:75
GlyphMap()=default
~GlyphMap()=default
const GlyphData * get(std::uint16_t codepoint) const
Definition: Font.cpp:75
void set(std::uint16_t codepoint, const GlyphData &glyph)
Store the GlyphData for a given Unicode codepoint.
Definition: Font.cpp:84
std::unique_ptr< std::array< GlyphData, 256 > > m_Data[256]
Definition: Font.h:92
NONCOPYABLE(GlyphMap)
MOVABLE(GlyphMap)
Storage for a bitmap font.
Definition: Font.h:50
FT_Library m_FreeType
Definition: Font.h:196
~CFont()=default
void BlendGlyphBitmapToTextureR8(const FT_Bitmap &bitmap, int targetX, int targetY)
Definition: Font.cpp:567
void UploadAtlasTextureToGPU(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: Font.cpp:511
float GetHeight() const
Definition: Font.cpp:92
bool AddFontFromPath(const OsPath &fontPath)
Definition: Font.cpp:195
std::unique_ptr< std::remove_pointer_t< FT_Stroker >, decltype(&ftStrokerDeleter)> UniqueFTStroker
Definition: Font.h:136
bool HasRGB() const
Definition: Font.h:95
CTexturePtr m_Texture
Definition: Font.h:160
float m_Height
Definition: Font.h:168
CFont(FT_Library library, const std::array< float, 256 > &gammaCorrection)
Definition: Font.h:52
float GetCharacterWidth(wchar_t c)
Definition: Font.cpp:102
static constexpr float MINIMAL_FONT_SIZE_ANTIALIASING
Some fonts are not rendered well at small sizes, so we set a minimum size.
Definition: Font.h:206
int m_AtlasY
Definition: Font.h:175
std::optional< CVector2D > GenerateGlyphBitmap(FT_Glyph &glyph, std::uint16_t codepoint, FT_Render_Mode renderMode, CVector2D offset, const float baselineInAtlas)
Definition: Font.cpp:480
void BlendGlyphBitmapToTextureRGBA(const FT_Bitmap &bitmap, int targetX, int targetY, std::uint8_t r, std::uint8_t g, std::uint8_t b)
Definition: Font.cpp:536
CRect m_Bounds
Definition: Font.h:171
int m_AtlasSize
Definition: Font.h:190
std::optional< CVector2D > GenerateStrokeGlyphBitmap(const FT_Glyph &glyph, std::uint16_t codepoint, FT_Render_Mode renderMode, const float baselineInAtlas)
Definition: Font.cpp:434
const GlyphData * GetGlyph(std::uint16_t i)
Definition: Font.cpp:329
NONCOPYABLE(CFont)
bool m_IsTextureInitialized
Definition: Font.h:183
MOVABLE(CFont)
void CalculateStringSize(const wchar_t *string, float &w, float &h)
Definition: Font.cpp:110
std::vector< UniqueFTFace > m_Faces
Definition: Font.h:198
GlyphMap m_Glyphs
Definition: Font.h:165
UniqueFTStroker m_Stroker
Definition: Font.h:199
int m_AtlasX
Definition: Font.h:174
int m_AtlasPadding
Definition: Font.h:181
std::unique_ptr< std::uint8_t[]> m_TexData
Definition: Font.h:187
int m_AtlasHeight
Definition: Font.h:179
Renderer::Backend::Format m_TextureFormat
Definition: Font.h:188
void BlendGlyphBitmapToTexture(const FT_Bitmap &bitmap, int targetX, int targetY, std::uint8_t r, std::uint8_t g, std::uint8_t b)
Definition: Font.cpp:526
static void ftFaceDeleter(FT_Face face)
Definition: Font.h:125
float m_FontSize
Definition: Font.h:192
const GlyphData * ExtractAndGenerateGlyph(std::uint16_t codepoint)
Definition: Font.cpp:335
std::vector< std::unique_ptr< std::uint8_t[], AlignedDeleter > > m_FontsData
Definition: Font.h:197
int m_TextureFormatStride
Definition: Font.h:189
bool ConstructAtlasTexture(Renderer::Backend::IDevice *device)
Definition: Font.cpp:268
std::unique_ptr< std::remove_pointer_t< FT_Face >, decltype(&ftFaceDeleter)> UniqueFTFace
Definition: Font.h:135
Renderer::Backend::Sampler::Desc ChooseTextureFormatAndSampler()
Definition: Font.cpp:245
std::reference_wrapper< const std::array< float, 256 > > m_GammaCorrectionLUT
Definition: Font.h:194
float m_Scale
Definition: Font.h:185
std::string m_FontName
Definition: Font.h:193
CTexturePtr GetTexture() const
Definition: Font.h:117
static void ftStrokerDeleter(FT_Stroker stroker)
Definition: Font.h:130
float GetCapHeight()
In typography, cap height is the height of a capital letter above the baseline for a particular typef...
Definition: Font.cpp:96
int m_AtlasWidth
Definition: Font.h:178
bool m_HasRGB
Definition: Font.h:163
void GetGlyphBounds(float &x0, float &y0, float &x1, float &y1) const
Definition: Font.h:109
float GetStrokeWidth() const
Definition: Font.h:107
bool m_IsDirty
Definition: Font.h:182
float m_StrokeWidth
Definition: Font.h:184
void InitalizeAtlasTextureIfNeeded(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: Font.cpp:316
bool SetFontParams(Renderer::Backend::IDevice *device, const std::string &fontName, float size, float strokeWidth, float scale)
Definition: Font.cpp:162
Rectangle class used for screen rectangles.
Definition: Rect.h:31
float left
Returning CVector2D representing each corner.
Definition: Rect.h:124
float bottom
Definition: Rect.h:124
float top
Definition: Rect.h:124
float right
Definition: Rect.h:124
Definition: Vector2D.h:32
Definition: path.h:84
Definition: IDeviceCommandContext.h:46
Definition: IDevice.h:64
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:25
Definition: Font.h:44
Definition: Canvas2D.h:30
Format
Definition: Format.h:28
library
Definition: tests.py:24
Definition: Font.h:58
float x0
Definition: Font.h:60
float v1
Definition: Font.h:59
float v0
Definition: Font.h:59
float u0
Definition: Font.h:59
float xadvance
Definition: Font.h:61
float y0
Definition: Font.h:60
float yadvance
Definition: Font.h:62
float y1
Definition: Font.h:60
float x1
Definition: Font.h:60
std::uint8_t defined
Definition: Font.h:63
FT_Face face
Definition: Font.h:64
float u1
Definition: Font.h:59
Definition: Sampler.h:54