Line data Source code
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"
22 : #include "lib/allocators/shared_ptr.h"
23 : #include "lib/code_annotation.h"
24 : #include "lib/os_path.h"
25 : #include "maths/Rect.h"
26 : #include "renderer/backend/Format.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 :
41 : class CVector2D;
42 : namespace Renderer::Backend { class IDevice; }
43 : namespace Renderer::Backend { class IDeviceCommandContext; }
44 : namespace Renderer::Backend::Sampler { struct Desc; }
45 :
46 : /**
47 : * Storage for a bitmap font. Loaded by CFontManager.
48 : */
49 : class CFont
50 : {
51 : public:
52 5 : CFont(FT_Library library, const std::array<float, 256>& gammaCorrection) : m_FreeType{library}, m_GammaCorrectionLUT{gammaCorrection} {}
53 20 : ~CFont() = default;
54 10 : MOVABLE(CFont);
55 : NONCOPYABLE(CFont);
56 :
57 2012 : 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 : */
74 : class GlyphMap
75 : {
76 : public:
77 5 : GlyphMap() = default;
78 2570 : ~GlyphMap() = default;
79 1285 : MOVABLE(GlyphMap);
80 : NONCOPYABLE(GlyphMap);
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 0 : 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 0 : void GetGlyphBounds(float& x0, float& y0, float& x1, float& y1) const
110 : {
111 0 : x0 = m_Bounds.left;
112 0 : y0 = m_Bounds.top;
113 0 : x1 = m_Bounds.right;
114 0 : y1 = m_Bounds.bottom;
115 : }
116 :
117 0 : CTexturePtr GetTexture() const { return m_Texture; }
118 : void InitalizeAtlasTextureIfNeeded(
119 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
120 : void UploadAtlasTextureToGPU(
121 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
122 : const GlyphData* GetGlyph(std::uint16_t i);
123 :
124 : private:
125 5 : static void ftFaceDeleter(FT_Face face)
126 : {
127 5 : FT_Done_Face(face);
128 5 : }
129 :
130 0 : static void ftStrokerDeleter(FT_Stroker stroker)
131 : {
132 0 : FT_Stroker_Done(stroker);
133 0 : }
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);
157 : bool ConstructAtlasTexture(Renderer::Backend::IDevice* device);
158 : Renderer::Backend::Sampler::Desc ChooseTextureFormatAndSampler();
159 :
160 : CTexturePtr m_Texture;
161 :
162 : // True if RGBA, false if ALPHA.
163 : bool m_HasRGB{true};
164 :
165 : GlyphMap m_Glyphs;
166 :
167 : // Height of a capital letter, roughly.
168 : float m_Height;
169 :
170 : // Bounding box of all glyphs
171 : CRect m_Bounds;
172 :
173 : // The position for the next glyph in the texture atlas.
174 : int m_AtlasX;
175 : int m_AtlasY;
176 :
177 : // Size of the texture atlas.
178 : int m_AtlasWidth;
179 : int m_AtlasHeight;
180 :
181 : int m_AtlasPadding;
182 : bool m_IsDirty{false};
183 : bool m_IsTextureInitialized{false};
184 : float m_StrokeWidth{0.0f};
185 : float m_Scale{1.0f};
186 :
187 : std::unique_ptr<std::uint8_t[]> m_TexData;
188 : Renderer::Backend::Format m_TextureFormat{Renderer::Backend::Format::R8G8B8A8_UNORM};
189 : int m_TextureFormatStride{4};
190 : int m_AtlasSize{0};
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;
199 : UniqueFTStroker m_Stroker{nullptr, &ftStrokerDeleter};
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
|