MyGUI 3.4.4
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
14#ifdef MYGUI_USE_FREETYPE
15 #include <ft2build.h>
16 #include FT_FREETYPE_H
17
18 #ifdef MYGUI_MSDF_FONTS
19namespace msdfgen
20{
21 class FontHandle;
22 class Shape;
23}
24 #endif
25
26#endif // MYGUI_USE_FREETYPE
27
28#include <unordered_map>
29
30namespace MyGUI
31{
32
34 {
36
37 public:
39
40 void deserialization(xml::ElementPtr _node, Version _version) override;
41
42 // Returns the glyph info for the specified code point, or the glyph info for a substitute glyph if the code point does not
43 // exist in this font. Returns nullptr if there is a problem with the font.
44 const GlyphInfo* getGlyphInfo(Char _id) const override;
45
46 ITexture* getTextureFont() const override;
47
48 // получившаяся высота при генерации в пикселях
49 int getDefaultHeight() const override;
50
51 // update texture after render device lost event
52 void textureInvalidate(ITexture* _texture) override;
53
54 // Returns a collection of code-point ranges that are supported by this font. Each range is specified as [first, second];
55 // for example, a range containing a single code point will have the same value for both first and second.
56 std::vector<std::pair<Char, Char>> getCodePointRanges() const;
57
58 // Returns the code point that is used as a substitute for code points that don't exist in the font. The default substitute
59 // code point is FontCodeType::NotDefined, but it can be customized in the font definition file.
61
62 // создаение ресурса по текущим значениям
63 void initialise();
64
65 void setSource(std::string_view _value);
66 void setShader(std::string_view _value);
67 void setSize(float _value);
68 void setResolution(unsigned int _value);
69 void setHinting(std::string_view _value);
70 void setAntialias(bool _value);
71 void setTabWidth(float _value);
72 void setOffsetHeight(int _value);
73 void setSubstituteCode(int _value);
74 void setDistance(int _value);
75 void setMsdfMode(bool _value);
76 void setMsdfRange(int _value);
77
78 void addCodePointRange(Char _first, Char _second);
79 void removeCodePointRange(Char _first, Char _second);
80
81#ifdef MYGUI_USE_FREETYPE
82 private:
83 enum Hinting
84 {
85 HintingUseNative,
86 HintingForceAuto,
87 HintingDisableAuto,
88 HintingDisableAll
89 };
90
91 void addCodePoint(Char _codePoint);
92
93 // The following variables are set directly from values specified by the user.
94 std::string mSource; // Source (filename) of the font.
95 std::string mShader; // Optional shader, applied to the font.
96 float mSize{0}; // Size of the font, in points (there are 72 points per inch).
97 unsigned int mResolution{96}; // Resolution of the font, in pixels per inch.
98 Hinting mHinting{HintingUseNative}; // What type of hinting to use when rendering the font.
99 bool mAntialias{
100 false}; // Whether or not to anti-alias the font by copying its alpha channel to its luminance channel.
101 float mSpaceWidth{0.0f}; // The width of a "Space" character, in pixels. If zero, the default width is used.
102 int mGlyphSpacing{-1}; // How far apart the glyphs are placed from each other in the font texture, in pixels.
103 float mTabWidth{0.0f}; // The width of the "Tab" special character, in pixels.
104 int mOffsetHeight{
105 0}; // How far up to nudge text rendered in this font, in pixels. May be negative to nudge text down.
106 Char mSubstituteCodePoint{static_cast<Char>(
107 FontCodeType::
108 NotDefined)}; // The code point to use as a substitute for code points that don't exist in the font.
109 bool mMsdfMode{
110 false}; // Signed distance field texture, designed to be used with shader (see https://github.com/Chlumsky/msdfgen)
111 int mMsdfRange{2}; // Gragient area range in pixels for msdf mode (higher range is required for thick outlines)
112
113 // The following variables are calculated automatically.
114 int mDefaultHeight{0}; // The nominal height of the font in pixels.
115 GlyphInfo* mSubstituteGlyphInfo{
116 nullptr}; // The glyph info to use as a substitute for code points that don't exist in the font.
117 MyGUI::ITexture* mTexture{nullptr}; // The texture that contains all of the rendered glyphs in the font.
118
119 // The following constants used to be mutable, but they no longer need to be. Do not modify their values!
120 static const int
121 mDefaultGlyphSpacing; // How far apart the glyphs are placed from each other in the font texture, in pixels.
122 static const float mDefaultTabWidth; // Default "Tab" width, used only when tab width is no specified.
123 static const float
124 mSelectedWidth; // The width of the "Selected" and "SelectedBack" special characters, in pixels.
125 static const float mCursorWidth; // The width of the "Cursor" special character, in pixels.
126
127 private:
128 // A map of code points to glyph indices.
129 using CharMap = std::map<Char, FT_UInt>;
130
131 // A map of glyph indices to glyph info objects.
132 using GlyphMap = std::unordered_map<Char, GlyphInfo>;
133
134 // A map of glyph heights to the set of paired glyph indices and glyph info objects that are of that height.
135 using GlyphHeightMap = std::map<int, std::map<FT_UInt, GlyphInfo*>>;
136
137 template<bool LAMode, bool Antialias>
138 void initialiseFreeType();
139
140 // Loads the font face as specified by mSource, mSize, and mResolution. Automatically adjusts code-point ranges according
141 // to the capabilities of the font face.
142 // Returns a handle to the FreeType face object for the face, or nullptr if the face could not be loaded.
143 // Keeps the font file loaded in memory and stores its location in _fontBuffer. The caller is responsible for freeing this
144 // buffer when it is done using the face by calling delete[] on the buffer after calling FT_Done_Face() on the face itself.
145 FT_Face loadFace(const FT_Library& _ftLibrary, uint8*& _fontBuffer);
146
147 // Wraps the current texture coordinates _texX and _texY to the beginning of the next line if the specified glyph width
148 // doesn't fit at the end of the current line. Automatically takes the glyph spacing into account.
149 void autoWrapGlyphPos(int _glyphWidth, int _texWidth, int _lineHeight, int& _texX, int& _texY) const;
150
151 // Creates a GlyphInfo object using the specified information.
152 GlyphInfo createFaceGlyphInfo(Char _codePoint, int _fontAscent, FT_GlyphSlot _glyph) const;
153
154 // Creates a glyph with the specified glyph index and assigns it to the specified code point.
155 // Automatically updates _glyphHeightMap, mCharMap, and mGlyphMap with data from the new glyph..
156 int createGlyph(FT_UInt _glyphIndex, const GlyphInfo& _glyphInfo, GlyphHeightMap& _glyphHeightMap);
157
158 // Creates a glyph with the specified index from the specified font face and assigns it to the specified code point.
159 // Automatically updates _glyphHeightMap with data from the newly created glyph.
160 int createFaceGlyph(
161 FT_UInt _glyphIndex,
162 Char _codePoint,
163 int _fontAscent,
164 const FT_Face& _ftFace,
165 FT_Int32 _ftLoadFlags,
166 GlyphHeightMap& _glyphHeightMap);
167
168 // Renders all of the glyphs in _glyphHeightMap into the specified texture buffer using data from the specified font face.
169 template<bool LAMode, bool Antialias>
170 void renderGlyphs(
171 const GlyphHeightMap& _glyphHeightMap,
172 const FT_Library& _ftLibrary,
173 const FT_Face& _ftFace,
174 FT_Int32 _ftLoadFlags,
175 uint8* _texBuffer,
176 int _texWidth,
177 int _texHeight);
178
179 // Renders the glyph described by the specified glyph info according to the specified parameters.
180 // Supports two types of rendering, depending on the value of UseBuffer: Texture block transfer and rectangular color fill.
181 // The _luminance0 value is used for even-numbered columns (from zero), while _luminance1 is used for odd-numbered ones.
182 template<bool LAMode, bool UseBuffer, bool Antialias>
183 void renderGlyph(
184 GlyphInfo& _info,
185 uint8 _luminance0,
186 uint8 _luminance1,
187 uint8 _alpha,
188 int _lineHeight,
189 uint8* _texBuffer,
190 int _texWidth,
191 int _texHeight,
192 int& _texX,
193 int& _texY,
194 uint8* _glyphBuffer = nullptr);
195
196 CharMap mCharMap; // A map of code points to glyph indices.
197 GlyphMap mGlyphMap; // A map of code points to glyph info objects.
198
199 #ifdef MYGUI_MSDF_FONTS
200 GlyphInfo createMsdfFaceGlyphInfo(
201 Char _codePoint,
202 const msdfgen::Shape& _shape,
203 double _advance,
204 int _fontAscent);
205 int createMsdfGlyph(const GlyphInfo& _glyphInfo, GlyphHeightMap& _glyphHeightMap);
206 int createMsdfFaceGlyph(
207 Char _codePoint,
208 int _fontAscent,
209 msdfgen::FontHandle* _fontHandle,
210 GlyphHeightMap& _glyphHeightMap);
211
212 void renderMsdfGlyphs(
213 const GlyphHeightMap& _glyphHeightMap,
214 msdfgen::FontHandle* _fontHandle,
215 uint8* _texBuffer,
216 int _texWidth,
217 int _texHeight);
218 #endif
219
220#endif // MYGUI_USE_FREETYPE
221 };
222
223} // namespace MyGUI
224
225#endif // MYGUI_RESOURCE_TRUE_TYPE_FONT_H_
#define MYGUI_EXPORT
#define MYGUI_RTTI_DERIVED(DerivedType)
Definition MyGUI_RTTI.h:69
void setShader(std::string_view _value)
void setResolution(unsigned int _value)
void setSource(std::string_view _value)
void textureInvalidate(ITexture *_texture) override
const GlyphInfo * getGlyphInfo(Char _id) const override
void removeCodePointRange(Char _first, Char _second)
void addCodePointRange(Char _first, Char _second)
void setHinting(std::string_view _value)
ITexture * getTextureFont() const override
std::vector< std::pair< Char, Char > > getCodePointRanges() const
void deserialization(xml::ElementPtr _node, Version _version) override
Element * ElementPtr
unsigned int Char
Definition MyGUI_Types.h:50