MyGUI 3.5.3
MyGUI_ResourceTrueTypeFont.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_RESOURCE_TRUE_TYPE_FONT_H_
8#define MYGUI_RESOURCE_TRUE_TYPE_FONT_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_ITexture.h"
12#include "MyGUI_IFont.h"
13#include <cstddef>
14
15#ifdef MYGUI_USE_FREETYPE
16
17using FT_UInt = unsigned int;
18using FT_Int32 = signed int;
19struct FT_FaceRec_;
20using FT_Face = FT_FaceRec_*;
21struct FT_LibraryRec_;
22using FT_Library = FT_LibraryRec_*;
23struct FT_GlyphSlotRec_;
24using FT_GlyphSlot = FT_GlyphSlotRec_*;
25
26 #ifdef MYGUI_MSDF_FONTS
27namespace msdfgen
28{
29
30 class FontHandle;
31 class Shape;
32
33}
34 #endif
35
36#endif // MYGUI_USE_FREETYPE
37
38#include <unordered_map>
39
40namespace MyGUI
41{
42
43 class MYGUI_EXPORT ResourceTrueTypeFont : public IFont, public ITextureInvalidateListener
44 {
45 MYGUI_RTTI_DERIVED(ResourceTrueTypeFont)
46
47 public:
48 ~ResourceTrueTypeFont() override;
49
50 void deserialization(xml::ElementPtr _node, Version _version) override;
51
52 // Returns the glyph info for the specified code point, or the glyph info for a substitute glyph if the code point does not
53 // exist in this font. Returns nullptr if there is a problem with the font.
54 const GlyphInfo* getGlyphInfo(Char _id) const override;
55
56 ITexture* getTextureFont() const override;
57
58 // Resulting height in pixels after generation
59 int getDefaultHeight() const override;
60
61 // update texture after render device lost event
62 void textureInvalidate(ITexture* _texture) override;
63
64 float getKerning(Char _left, Char _right) const override;
65
66 // Returns a collection of code-point ranges that are supported by this font. Each range is specified as [first, second];
67 // for example, a range containing a single code point will have the same value for both first and second.
68 std::vector<std::pair<Char, Char>> getCodePointRanges() const;
69
70 // Returns the code point that is used as a substitute for code points that don't exist in the font. The default substitute
71 // code point is FontCodeType::NotDefined, but it can be customized in the font definition file.
72 Char getSubstituteCodePoint() const;
73
74 void initialise();
75
76 void setSource(std::string_view _value);
77 void setShader(std::string_view _value);
78 void setSize(float _value);
79 void setResolution(unsigned int _value);
80 void setHinting(std::string_view _value);
81 void setAntialias(bool _value);
82 void setTabWidth(float _value);
83 void setOffsetHeight(int _value);
84 void setSubstituteCode(int _value);
85 void setDistance(int _value);
86 void setMsdfMode(bool _value);
87 void setMsdfRange(int _value);
88 void setKerningEnabled(bool _value);
89 void setDpiScale(float _value);
90 void setAutoDpi(bool _value);
91
92 void addCodePointRange(Char _first, Char _second);
93 void removeCodePointRange(Char _first, Char _second);
94
95#ifdef MYGUI_USE_FREETYPE
96 private:
97 enum Hinting
98 {
99 // Use native hints with auto-hinter fallback (FT_LOAD_DEFAULT). Best default for most fonts
100 HintingUseNative,
101 // Always use FreeType's auto-hinter (FT_LOAD_FORCE_AUTOHINT). Override bad font hints
102 HintingForceAuto,
103 // Use only native hints, disable the auto-hinter (FT_LOAD_NO_AUTOHINT). For fonts where auto-hinter degrades quality
104 HintingDisableAuto,
105 // Mono font with some anti-aliasing. Best for very small sizes (FT_LOAD_TARGET_MONO + FT_RENDER_MODE_NORMAL)
106 HintingMonochrome,
107 // Disable all hinting entirely (FT_LOAD_NO_HINTING). Use for large/display text where shape fidelity matters more than crispness
108 HintingDisableAll,
109 };
110
111 void addCodePoint(Char _codePoint);
112
113 // The following variables are set directly from values specified by the user.
114 std::string mSource; // Source (filename) of the font.
115 std::string mShader; // Optional shader, applied to the font.
116 float mSize{0}; // Size of the font, in points (there are 72 points per inch).
117 unsigned int mResolution{96}; // Resolution of the font, in pixels per inch.
118 Hinting mHinting{HintingUseNative}; // What type of hinting to use when rendering the font.
119 bool mAntialias{
120 false}; // Whether or not to anti-alias the font by copying its alpha channel to its luminance channel.
121 float mSpaceWidth{0.0f}; // The width of a "Space" character, in pixels. If zero, the default width is used.
122 int mGlyphSpacing{-1}; // How far apart the glyphs are placed from each other in the font texture, in pixels.
123 float mTabWidth{0.0f}; // The width of the "Tab" special character, in pixels.
124 int mOffsetHeight{
125 0}; // How far up to nudge text rendered in this font, in pixels. May be negative to nudge text down.
126 Char mSubstituteCodePoint{static_cast<Char>(
127 FontCodeType::
128 NotDefined)}; // The code point to use as a substitute for code points that don't exist in the font.
129 bool mMsdfMode{
130 false}; // Signed distance field texture, designed to be used with shader (see https://github.com/Chlumsky/msdfgen)
131 int mMsdfRange{2}; // Gradient area range in pixels for msdf mode (higher range is required for thick outlines)
132 bool mKerningEnabled{true}; // Whether kerning is enabled for this font.
133 float mDpiScale{1.0f}; // DPI scale factor for generating a higher-resolution atlas.
134 bool mAutoDpi{true}; // When true, use the global DPI scale from Gui instead of mDpiScale.
135
136 // The following variables are calculated automatically.
137 int mDefaultHeight{0}; // The nominal height of the font in pixels.
138 GlyphInfo* mSubstituteGlyphInfo{
139 nullptr}; // The glyph info to use as a substitute for code points that don't exist in the font.
140 MyGUI::ITexture* mTexture{nullptr}; // The texture that contains all of the rendered glyphs in the font.
141
142 // The following constants used to be mutable, but they no longer need to be. Do not modify their values!
143 static const int
144 mDefaultGlyphSpacing; // How far apart the glyphs are placed from each other in the font texture, in pixels.
145 static const float mDefaultTabWidth; // Default "Tab" width, used only when tab width is no specified.
146 static const float
147 mSelectedWidth; // The width of the "Selected" and "SelectedBack" special characters, in pixels.
148 static const float mCursorWidth; // The width of the "Cursor" special character, in pixels.
149
150 private:
151 // A map of code points to glyph indices.
152 using CharMap = std::map<Char, FT_UInt>;
153
154 // A map of glyph indices to glyph info objects.
155 using GlyphMap = std::unordered_map<Char, GlyphInfo>;
156
157 // A map of glyph heights to the set of paired glyph indices and glyph info objects that are of that height.
158 using GlyphHeightMap = std::map<int, std::map<FT_UInt, GlyphInfo*>>;
159
160 template<bool LAMode, bool Antialias>
161 void initialiseFreeType();
162
163 // Loads the font face as specified by mSource, mSize, and mResolution. Automatically adjusts code-point ranges according
164 // to the capabilities of the font face.
165 // Returns a handle to the FreeType face object for the face, or nullptr if the face could not be loaded.
166 // Stores the font file in _fontBuffer, which must remain unchanged until FT_Done_Face() is called on the face.
167 FT_Face loadFace(const FT_Library& _ftLibrary, std::vector<std::byte>& _fontBuffer);
168
169 // Loads kerning pairs from the font face.
170 void loadKerning(const FT_Face& _ftFace);
171
172 // Wraps the current texture coordinates _texX and _texY to the beginning of the next line if the specified glyph width
173 // doesn't fit at the end of the current line. Automatically takes the glyph spacing into account.
174 void autoWrapGlyphPos(int _glyphWidth, int _texWidth, int _lineHeight, int& _texX, int& _texY) const;
175
176 // Creates a GlyphInfo object using the specified information.
177 GlyphInfo createFaceGlyphInfo(Char _codePoint, int _fontAscent, FT_GlyphSlot _glyph) const;
178
179 // Creates a glyph with the specified glyph index and assigns it to the specified code point.
180 // Automatically updates _glyphHeightMap, mCharMap, and mGlyphMap with data from the new glyph..
181 int createGlyph(
182 FT_UInt _glyphIndex,
183 const GlyphInfo& _glyphInfo,
184 GlyphHeightMap& _glyphHeightMap,
185 float _scale = -1.0);
186
187 // Creates a glyph with the specified index from the specified font face and assigns it to the specified code point.
188 // Automatically updates _glyphHeightMap with data from the newly created glyph.
189 int createFaceGlyph(
190 FT_UInt _glyphIndex,
191 Char _codePoint,
192 int _fontAscent,
193 const FT_Face& _ftFace,
194 FT_Int32 _ftLoadFlags,
195 GlyphHeightMap& _glyphHeightMap);
196
197 // Renders all of the glyphs in _glyphHeightMap into the specified texture buffer using data from the specified font face.
198 template<bool LAMode, bool Antialias>
199 void renderGlyphs(
200 const GlyphHeightMap& _glyphHeightMap,
201 const FT_Library& _ftLibrary,
202 const FT_Face& _ftFace,
203 FT_Int32 _ftLoadFlags,
204 uint8* _texBuffer,
205 int _texWidth,
206 int _texHeight);
207
208 // Renders the glyph described by the specified glyph info according to the specified parameters.
209 // Supports two types of rendering, depending on the value of UseBuffer: Texture block transfer and rectangular color fill.
210 // The _luminance0 value is used for even-numbered columns (from zero), while _luminance1 is used for odd-numbered ones.
211 template<bool LAMode, bool UseBuffer, bool Antialias>
212 void renderGlyph(
213 GlyphInfo& _info,
214 uint8 _luminance0,
215 uint8 _luminance1,
216 uint8 _alpha,
217 int _lineHeight,
218 uint8* _texBuffer,
219 int _texWidth,
220 int _texHeight,
221 int& _texX,
222 int& _texY,
223 int _bitmapWidth,
224 int _bitmapHeight,
225 uint8* _glyphBuffer = nullptr);
226
227 CharMap mCharMap; // A map of code points to glyph indices.
228 GlyphMap mGlyphMap; // A map of code points to glyph info objects.
229 KerningMap mKerningMap; // A map of kerning pairs to kerning values.
230
231 #ifdef MYGUI_MSDF_FONTS
232 double mMsdfScale{1.0};
233
234 GlyphInfo createMsdfFaceGlyphInfo(
235 Char _codePoint,
236 const msdfgen::Shape& _shape,
237 double _advance,
238 int _fontAscent);
239 int createMsdfFaceGlyph(
240 FT_UInt _glyphIndex,
241 Char _codePoint,
242 int _fontAscent,
243 msdfgen::FontHandle* _fontHandle,
244 GlyphHeightMap& _glyphHeightMap);
245
246 void renderMsdfGlyphs(
247 const GlyphHeightMap& _glyphHeightMap,
248 msdfgen::FontHandle* _fontHandle,
249 uint8* _texBuffer,
250 int _texWidth,
251 int _texHeight);
252 #endif
253
254#endif // MYGUI_USE_FREETYPE
255 };
256
257} // namespace MyGUI
258
259#endif // MYGUI_RESOURCE_TRUE_TYPE_FONT_H_
#define MYGUI_EXPORT
#define MYGUI_RTTI_DERIVED(DerivedType)
Definition MyGUI_RTTI.h:67
std::unordered_map< std::pair< Char, Char >, float, KerningPairHash > KerningMap
unsigned int Char
Definition MyGUI_Types.h:49