MyGUI: MyGUI_ResourceTrueTypeFont.h Source File

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